1.RecyclerView:不仅可以纵向滑动,也可以横向滑动,一个加强的LIstView。
需要在dependencies中加入
compile ‘com.android.support:recyclerview-v7:25.1.0‘
2.在布局中加入控件
<android.support.v7.widget.RecyclerView android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/recyclerView"/>
3.同样需要一个子布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="50dp"> <ImageView android:layout_height="50dp" android:layout_width="80dp" android:id="@+id/imageView" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/textView" android:layout_height="50dp" android:layout_width="0dp" android:layout_weight="1" android:text="111" android:textColor="#000000" android:textSize="20dp" android:gravity="center_horizontal|center_vertical" /> </LinearLayout>
4.定义的适配器
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{ List<MuInfo> data; Context context; public RecyclerAdapter(List<MuInfo> data, Context context) { this.data = data; this.context = context; } @Override public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view=LayoutInflater.from(context).inflate(R.layout.itme_layout, parent, false); final MyViewHolder holder = new MyViewHolder(view); holder.imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int postion=holder.getAdapterPosition(); Toast.makeText(context,postion+"--"+data.get(postion).getName(),Toast.LENGTH_SHORT).show(); } }); holder.MuinfoitemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int postion=holder.getAdapterPosition(); Toast.makeText(context,postion+"--"+data.get(postion).getName()+"****",Toast.LENGTH_SHORT).show(); } }); return holder; } @Override public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, final int position) { MuInfo muInfo=data.get(position); holder.textView.setText(muInfo.getName()); holder.imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.a)); } @Override public int getItemCount() { return data.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { View MuinfoitemView; ImageView imageView; TextView textView; public MyViewHolder(View itemView) { super(itemView); MuinfoitemView=itemView; imageView= (ImageView) itemView.findViewById(R.id.imageView); textView= (TextView) itemView.findViewById(R.id.textView); } } }
5.在MainActivity中实现:下面加粗的控制RecyclerView为横向滑动,如果没有这一行,则默认为纵向滑动,和普通的ListView一样。
public class MainActivity extends AppCompatActivity {
List<MuInfo>data=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MuInfo muInfo=new MuInfo("fgehj","fghjkl");
MuInfo muInfo1=new MuInfo("fghdfj","fghjwertkl");
for(int i=0;i<15;i++){
data.add(muInfo);
data.add(muInfo1);
}
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
RecyclerAdapter adapter=new RecyclerAdapter(data,getApplicationContext());
recyclerView.setAdapter(adapter);
}
}
6.实体类也加上吧:
public class MuInfo { private String name; private String address; public MuInfo(String name, String address) { this.name = name; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
二、
1.改变子布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content"> <ImageView android:layout_height="50dp" android:layout_width="80dp" android:id="@+id/imageView" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/textView" android:layout_height="wrap_content" android:layout_width="80dp" android:text="111" android:textColor="#000000" android:textSize="20dp" android:gravity="center_horizontal|center_vertical" /> </LinearLayout>
2.
for(int i=0;i<80;i++){ Random random=new Random(); int x=random.nextInt(5)+1; String s="好美的花"; for(int j=0;j<x;j++){ s=s+"好美的花"; } data.add(new MuInfo(s,s)); } RecyclerView recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
// 3:控制显示为3列 StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); RecyclerAdapter adapter=new RecyclerAdapter(data,getApplicationContext()); recyclerView.setAdapter(adapter);
大致如:但是滑动时,并不会出现3列滑动不一致动态效果。
三:如果实现三列,能够单独滑动?
原文:http://www.cnblogs.com/galibujianbusana/p/6394751.html