1. 程式人生 > >Android 混排效果之 ScrollView 巢狀 RecyclerView 巢狀gridview 實現listview 巢狀gridview 效果

Android 混排效果之 ScrollView 巢狀 RecyclerView 巢狀gridview 實現listview 巢狀gridview 效果

RecyclerView 出現以後 很受大家歡迎 漂亮流暢的列表 簡單的操作 可以幫我們完成很多的列表 但是有一種listview 巢狀gridview 的效果 目前還是需要巢狀才能完成 現在就簡單說一下我的思路 本人的需求是例如手機淘寶頁 本人使用老套的實現模式 Scrollview+recyclerView+gridview 實現 重寫gridview 遮蔽recyclerview 滑動 利用Scrollview 進行重新整理

下面上程式碼


> <com.handmark.pulltorefresh.library.PullToRefreshScrollView
>         android:layout_width="match_parent"
> android:layout_height="match_parent" > android:fillViewport="true" > android:id="@+id/test_refreush"> > <RelativeLayout > android:layout_width="match_parent" > android:layout_height="match_parent" > android:orientation="vertical"
> > <ImageView > android:id="@+id/tttt_1" > android:layout_width="match_parent" > android:layout_height="300dp" /> > <android.support.v7.widget.RecyclerView > android:layout_below="@+id/tttt_1"
> android:id="@+id/atest_list" > android:layout_width="match_parent" > android:layout_height="match_parent"> > </android.support.v7.widget.RecyclerView> > </RelativeLayout> > > </com.handmark.pulltorefresh.library.PullToRefreshScrollView>

// activity

>  private RecyclerView atest_list;
>     private String auuid;
>     private String aappid;
>     private String aes;
>     private List<ATestBean.DataBean> mData = new ArrayList<>();
>     private MyTestAdapter adapter;
>     private PullToRefreshScrollView test_refreush;
>     private int no = 1;
>     @Override
>     protected void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.activity_atest);
>         atest_list = (RecyclerView) findViewById(R.id.atest_list);
>         test_refreush = (PullToRefreshScrollView) findViewById(R.id.test_refreush);
>         atest_list.setHasFixedSize(true);
>         atest_list.setNestedScrollingEnabled(false);
>         findViewById(R.id.rl_back).setOnClickListener(this);
>         LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
>         atest_list.setLayoutManager(manager);
>         adapter = new MyTestAdapter(context,mData);
>         atest_list.setAdapter(adapter);
>         test_refreush.setMode(PullToRefreshBase.Mode.BOTH);
>         test_refreush.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
>             @Override
>             public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
>                 no++;
>                 getShopList(no);
>             }
> 
>             @Override
>             public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
>                 no++;
>                 getShopList(no);
>             }
>         });
>         getShopList(no);
>     }
>     //解析資料
>      Gson gson = new Gson();
        ATestBean testBean = gson.fromJson(data, ATestBean.class);
        mData.addAll(testBean.getData());
        adapter.notifyDataSetChanged();
        if (test_refreush!=null&&test_refreush.isRefreshing()){
            test_refreush.onRefreshComplete();
        }
**重點內容**

recyclerView 的 Adapter

public class MyTestAdapter extends RecyclerView.Adapter<MyTestAdapter.ViewHolder1> {
    private final LayoutInflater inflater;
    Context context;
    public List<ATestBean.DataBean> data;
    OnClickRecyclerViewItemListener onClickRecyclerViewItemListener;

    public MyTestAdapter(Context context, List<ATestBean.DataBean> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public MyTestAdapter.ViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.home_listitem, parent, false);
        ViewHolder1 holder1 = new ViewHolder1(view);


        return holder1;

    }

    @Override
    public void onBindViewHolder(MyTestAdapter.ViewHolder1 holder, final int position) {
        if (!TextUtils.isEmpty(data.get(position).getName())){
            holder.home_listname.setText(data.get(position).getName());
        }else {
            holder.home_listname.setText("店鋪");
        }

            ATestGridAdapter adapter = new ATestGridAdapter(context,data.get(position).getServiceList());
        holder.home_grid.setAdapter(adapter);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder1 extends RecyclerView.ViewHolder{
        RoundImageView home_listimg;
        TextView home_listname,home_listxinnum,home_listdistance,home_listpeople;
        MyGridView home_grid;
        ImageView home_listxin,home_listzhifu,home_listbao,home_listtype;
        RelativeLayout home_listitem_layout;
        public ViewHolder1(View itemView) {
            super(itemView);
            home_listimg = (RoundImageView) itemView.findViewById(R.id.home_listimg);
            home_listname = (TextView) itemView.findViewById(R.id.home_listname);
            home_listxinnum = (TextView) itemView.findViewById(R.id.home_listxinnum);
            home_listdistance = (TextView) itemView.findViewById(R.id.home_listdistance);
            home_listpeople = (TextView) itemView.findViewById(R.id.home_listpeople);
            home_grid = (MyGridView) itemView.findViewById(R.id.home_grid);
            home_listxin = (ImageView) itemView.findViewById(R.id.home_listxin);
            home_listzhifu = (ImageView) itemView.findViewById(R.id.home_listzhifu);
            home_listbao = (ImageView) itemView.findViewById(R.id.home_listbao);
            home_listtype = (ImageView) itemView.findViewById(R.id.home_listtype);
            home_listitem_layout = (RelativeLayout) itemView.findViewById(R.id.home_listitem_layout);
        }
    }

    public interface OnClickRecyclerViewItemListener{
        void recyclerViewItem(int position);

    }

    public void setRecyclerViewItemListener(OnClickRecyclerViewItemListener onClickRecyclerViewItemListener){
        this.onClickRecyclerViewItemListener = onClickRecyclerViewItemListener;
    }
}
public class ATestGridAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    Context context;
    List<ATestBean.DataBean.ServiceListBean> serviceList;

    public ATestGridAdapter(Context context, List<ATestBean.DataBean.ServiceListBean> serviceList) {
        this.context = context;
        this.serviceList = serviceList;
        inflater = LayoutInflater.from(context);


    }

    @Override
    public int getCount() {
        return serviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return serviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder2 holder2;
        if (convertView==null){
            convertView = inflater.inflate(R.layout.gridview_item,parent,false);
            holder2 = new ViewHolder2();
            holder2.gridimg = (ImageView) convertView.findViewById(R.id.gridimg);
            holder2.grid_type = (TextView) convertView.findViewById(R.id.grid_type);
            holder2.grid_typecontent = (TextView) convertView.findViewById(R.id.grid_typecontent);
            holder2.grid_numbercontent = (TextView) convertView.findViewById(R.id.grid_numbercontent);
            holder2.home_gridtitle = (LinearLayout) convertView.findViewById(R.id.home_gridtitle);
            convertView.setTag(holder2);
        }else {
            holder2 = (ViewHolder2) convertView.getTag();
        }

        holder2.grid_type.setText(serviceList.get(position).getName());


        return convertView;
    }

    class ViewHolder2{
        ImageView gridimg;
        TextView grid_type,grid_typecontent,grid_numbercontent;
        LinearLayout home_gridtitle;
    }
}

// mygridview

public class MyGridView extends GridView {
    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

這裡寫圖片描述