先导好github上的项目。
Test_horizontal_list这个activity的代码:
public class Test_horizontal_list extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList<String> datas;
private MyRecyclerViewAdapter adapter;
private TextView tv_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_horizontal_list);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
datas=new ArrayList<>();
adapter=new MyRecyclerViewAdapter(Test_horizontal_list.this,datas);
for(int i=0;i<100;i++){
datas.add("Content_"+i);
}
//设置适配器
recyclerView.setAdapter(adapter);
//横向Item单独滑动
// PagerSnapHelper snapHelper = new PagerSnapHelper();
// snapHelper.attachToRecyclerView(recyclerView);
//layoutManager
recyclerView.setLayoutManager(new LinearLayoutManager(Test_horizontal_list.this, LinearLayoutManager.HORIZONTAL,false));
}
}
对应的activity_test_horizontal_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="3dp"
tools:context="com.example.jc.activity.greening_management.Test_horizontal_list">
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
适配器MyRecyclerViewAdapter
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> implements View.OnClickListener {
private final Context context;
private ArrayList<String> datas;
private Uri uri;
public MyRecyclerViewAdapter(Context context, ArrayList<String> datas) {
this.context=context;
this.datas=datas;
BigImageViewer.initialize(GlideImageLoader.with(context));
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_recyclerview, viewGroup, false);
return new MyRecyclerViewAdapter.MyViewHolder(view);
// View itemView=View.inflate(context, R.layout.item_recyclerview,null);
// return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
//根据位置i来得到对应的数据
String data=datas.get(i);
myViewHolder.tv_title.setText(data);
uri = Uri.parse("https://imusae-download.90sheji.com/imusae/download/2021/01/20/2568d27cc6e24ee949aa6e17b0bf4663.jpeg?sign=8ae470cbffc41b2e228d264c1f61dab1&t=61548d80&im" +
"ageMogr2/thumbnail/280x/crop/!x280-0a0/auto-orient/interlace/1/sharpen/1/quality/85/format/webp");
myViewHolder.mBigImage.showImage(uri);
myViewHolder.mBigImage.setOnClickListener(this);
}
@Override
public int getItemCount() {
return datas.size();
}
@Override
public void onClick(View v) {
MyDialog myDialog=new MyDialog(context,uri);
myDialog.show();
}
class MyViewHolder extends RecyclerView.ViewHolder{
private BigImageView mBigImage;
private TextView tv_title;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
mBigImage = (BigImageView) itemView.findViewById(R.id.mBigImage);
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
}
}
}
item_recyclerview.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:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="ExtraText">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.piasy.biv.view.BigImageView
android:id="@+id/mBigImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:initScaleType="auto"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
</com.github.piasy.biv.view.BigImageView>
<TextView
android:id="@+id/tv_title"
android:textColor="#000000"
android:text="Content"
android:layout_marginTop="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>