android 横向可滑动布局

时间:2021-12-19 19:44:00


使用的是RecyclerView(其实我感觉跟listview差不多),首先构造适配器:


/**
* Created by th2 on 2017/3/21.
* 横向和滑动适配器
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerHolder> {

private List<String> list;
private Context context;

public RecyclerViewAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}

@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_image, parent, false);

final RecyclerHolder holder = new RecyclerHolder(view);
holder.mImg = (ImageView) view.findViewById(R.id.image);

holder.mImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里取控件位置的时候使用 holder.getPosition()
}
});
return holder;
}

@Override
public void onBindViewHolder(final RecyclerHolder holder, final int position) {
//对布局内控件进行操作
MyApp.bitmapUtils.display(holder.mImg , list.get(position));
}

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


class RecyclerHolder extends RecyclerView.ViewHolder {

public RecyclerHolder(View itemView) {
super(itemView);
}

ImageView mImg;
}

主界面布局


<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:scrollbars="none" />



主界面代码


RecyclerView recyclerView = (RecyclerView) goods0View.findViewById(R.id.recycler_view);
//设置布局管理器
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(pictureList,getActivity());
recyclerView.setAdapter(adapter);



好了,横向可滑动布局完成