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">
>                      android:layout_width="match_parent"
>             android:layout_height="match_parent"
>             android:orientation="vertical">
>                              android:id="@+id/tttt_1"
>                 android:layout_width="match_parent"
>                 android:layout_height="300dp" />
>             .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">
>             .support.v7.widget.RecyclerView>
>         
> 
>     com.handmark.pulltorefresh.library.PullToRefreshScrollView>

// activity

>  private RecyclerView atest_list;
>     private String auuid;
>     private String aappid;
>     private String aes;
>     private List 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() {
>             @Override
>             public void onPullDownToRefresh(PullToRefreshBase refreshView) {
>                 no++;
>                 getShopList(no);
>             }
> 
>             @Override
>             public void onPullUpToRefresh(PullToRefreshBase 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 serviceList;

    public ATestGridAdapter(Context context, List 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);
    }
}

Android 混排效果之 ScrollView 嵌套 RecyclerView 嵌套gridview 实现listview 嵌套gridview 效果_第1张图片

你可能感兴趣的:(android开发)