如何使布局水平流动

时间:2018-08-09 12:22:50

标签: android

enter image description here

我怎么能水平排列这样的布局流程,我尝试通过拆分子文本并在每个视图分页器中显示每个子菜单来使用Viewpager,但是我不知道内容何时到达屏幕的末尾。整个事情看起来很乱。 android中是否有类似这样的现成实现?

1 个答案:

答案 0 :(得分:0)

尝试在任何片段或活动中使用回收站视图。

首先创建自定义适配器,如下面的代码。

public class RecyclerViewAdpater extends RecyclerView.Adapter<RecyclerViewAdpater.ItemViewHolder> {
List<String> mStringList = new ArrayList<>();// hear you can pass any pojo class object.
Context mContext;
public RecyclerViewAdpater(List<String> mStringList, Context mContext) {
    this.mStringList = mStringList;
    this.mContext = mContext;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
    return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
    // below code handle click event on recycler view item.
    final String str=mStringList.get(position); //  here your boject
    holder.textView.setText(str);

}

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

public class ItemViewHolder extends RecyclerView.ViewHolder {
    TextView textView;

    public ItemViewHolder(View itemView) {
        super(itemView);
        textView=itemView.findViewById(R.id.textView);
    }
}

}

之后,将回收站视图定义为xml。 下面的代码用于活动或片段。 adpter传递您的短信。

   final RecyclerViewAdpater adpater=new RecyclerViewAdpater(yourtextList,this);
    mRvData=findViewById(R.id.rvData);
    mRvData.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL,false));
    mRvData.setAdapter(adpater);

采用水平方式。

  mRvData.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL,false)); // you can apply vertical default is vertical

更多信息,请参见此链接。 https://www.androidhive.info/2016/01/android-working-with-recycler-view/