Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中RecyclerView上拉下拉,分割線,多條目的實例代碼

Android中RecyclerView上拉下拉,分割線,多條目的實例代碼

編輯:關於Android編程

//activity的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="baway.com.cj.myrecyclerview2.MainActivity">
  <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_above="@+id/progress"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
  </android.support.v4.widget.SwipeRefreshLayout>
  <ProgressBar
    android:id="@+id/progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:visibility="gone"
    android:layout_alignParentBottom="true"/>
</RelativeLayout>
item1的xml和item2的xml就一個背景顏色不同
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#f00000"
  tools:context="baway.com.cj.myrecyclerview2.MainActivity">
  <TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="20sp"
    android:gravity="center"
    android:textColor="#00ff00"/>
</RelativeLayout>
item2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#f000ff"
  tools:context="baway.com.cj.myrecyclerview2.MainActivity">
  <TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="20sp"
    android:gravity="center"
    android:textColor="#00ff00"/>
</RelativeLayout>
//activity,有上啦下拉,添加分割線,
package baway.com.cj.myrecyclerview2;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
  private RecyclerView recyclerView;
  private MainAdapter mainAdapter;
  private SwipeRefreshLayout swipeRefreshLayout;
  private LinearLayoutManager linearLayoutManager;
  private ProgressBar progress;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    initData();
  }
  private void initView() {
    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    progress = (ProgressBar)findViewById(R.id.progress);
    linearLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(linearLayoutManager);
    mainAdapter = new MainAdapter(this);
    mainAdapter.setOnClickListener(new MyItemOnClickListener() {
      @Override
      public void onItemClick(View view, int position) {
        Toast.makeText(MainActivity.this, " "+position, Toast.LENGTH_SHORT).show();
      }
    });
    recyclerView.setAdapter(mainAdapter);
    recyclerView.addItemDecoration(new MyItemDecoration());
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
    swipeRefreshLayout.setProgressViewOffset(true,0,200);
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
          @Override
          public void run() {
            swipeRefreshLayout.setRefreshing(false);
          }
        },3000);
      }
    });
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        int lastVisibleItemPosition = linearLayoutManager.findLastVisibleItemPosition();
        int itemCount = linearLayoutManager.getItemCount();
        if (lastVisibleItemPosition + 1 >= itemCount && dy > 0) {
          progress.setVisibility(View.VISIBLE);
          new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
              progress.setVisibility(View.GONE);
            }
          },3000);
        }
      }
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
      }
    });
  }
  private void initData() {
    ArrayList<String> strings = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
      strings.add("name "+i);
    }
    mainAdapter.setData(strings);
    mainAdapter.notifyDataSetChanged();
  }
}
//adpter有多條目展示
package baway.com.cj.myrecyclerview2;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
/**
 * Created by cj on 2017/1/12.
 */
public class MainAdapter extends RecyclerView.Adapter {
  private Context context;
  private ArrayList<String> datas = new ArrayList<>();
  private MyItemOnClickListener myItemOnClickListener;
  private final int TYPE_0 = 0;
  private final int TYPE_1 = 1;
  public MainAdapter(Context context) {
    this.context = context;
  }
  @Override
  public int getItemViewType(int position) {
    if (position %3 == 0) {
      return TYPE_0;
    } else {
      return TYPE_1;
    }
  }
  public void setOnClickListener(MyItemOnClickListener onClickListener) {
    this.myItemOnClickListener = onClickListener;
  }
  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_0) {
      View inflate = LayoutInflater.from(context).inflate(R.layout.item_main, null);
      return new MyViewHolder(inflate);
    } else {
      View inflate = LayoutInflater.from(context).inflate(R.layout.item_main2, null);
      return new MyViewHolder2(inflate);
    }
  }
  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (getItemViewType(position) == TYPE_0) {
      MyViewHolder myViewHolder = (MyViewHolder)holder;
      myViewHolder.name.setText(datas.get(position));
      myViewHolder.name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          myItemOnClickListener.onItemClick(v,position);
        }
      });
    } else {
      MyViewHolder2 myViewHolder = (MyViewHolder2)holder;
      myViewHolder.name.setText(datas.get(position));
      myViewHolder.name.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          myItemOnClickListener.onItemClick(v,position);
        }
      });
    }
  }
  @Override
  public int getItemCount() {
    return datas.size();
  }
  public void setData(ArrayList<String> data) {
    if (data == null) {
      return;
    }
    this.datas.addAll(data);
  }
  public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView name;
    public MyViewHolder(View itemView) {
      super(itemView);
      name = (TextView) itemView.findViewById(R.id.name);
    }
  }
  public class MyViewHolder2 extends RecyclerView.ViewHolder{
    public TextView name;
    public MyViewHolder2(View itemView) {
      super(itemView);
      name = (TextView) itemView.findViewById(R.id.name);
    }
  }
}
//自定義分割線繼承系統的 RecyclerView.ItemDecoration
package baway.com.cj.myrecyclerview2;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
/**
 * Created by cj on 2017/1/12.
 */
public class MyItemDecoration extends RecyclerView.ItemDecoration {
  private Paint paint;
  public MyItemDecoration() {
    paint = new Paint();
    paint.setColor(Color.BLUE);
  }
  @Override
  public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    //c.drawCircle(300,300,300,paint);
    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
      View childAt = parent.getChildAt(i);
      c.drawRect(childAt.getLeft(),childAt.getBottom(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint);
    }
  }
  @Override
  public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
//    int childCount = parent.getChildCount();
//    for (int i = 0; i < childCount; i++) {
//      View childAt = parent.getChildAt(i);
//      c.drawRect(childAt.getLeft(),childAt.getTop(),childAt.getWidth()+childAt.getLeft(),childAt.getBottom()+5,paint);
//    }
  }
  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    outRect.set(20,0,20,5);
  }
}

以上所述是小編給大家介紹的Android中RecyclerView上拉下拉,分割線,多條目的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved