【问题标题】:Android RecyclerView with LinearLayoutAndroid RecyclerView 与 LinearLayout
【发布时间】:2015-04-28 05:29:38
【问题描述】:

我一直在阅读有关 Google 的 Material Design 的文章,并遇到了新的 Android 版 RecyclerView。我一直在关注 Google 的教程,但他们的示例非常小,我想用我的 RecylerView 做更多动态的事情,我希望有人可以帮助解释如何做到这一点。

这是 Google 的 RecyclerView 适配器示例

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder((TextView)v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

我已在我的 Android 应用程序中实现了这一点,并且它成功运行,但他们使用的布局仅包含一个 TextView。

谁能解释我如何将这个示例用于具有多个文本视图的布局?

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="?attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="8dp"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_margin="8dp"/>

</LinearLayout>

如何单独访问 TextView 并可能更改 LinearLayout 的属性?

这是我一直关注的谷歌教程Google RecyclerView

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    您可以使用 findViewById 从膨胀的线性布局中提取 TextView。

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private String[] mDataset;
    
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView mTitle;
            public TextView mText
            public ViewHolder(LinearLayout v) {
                super(v);
                mTitle = (TextView) v.findViewById(R.id.title);
                mText = (TextView) v.findViewById(R.id.text);
            }
        }
        .....
        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, parent, false);
            // set the view's size, margins, paddings and layout parameters
            ViewHolder vh = new ViewHolder((LinearLayout)v);
            return vh;
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.mTitle.setText(....);
            holder.mText.setText(.....);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多