【问题标题】:Custom View to be Used as a Nested View or as a RecyclerView Item view in Android在 Android 中用作嵌套视图或 RecyclerView 项视图的自定义视图
【发布时间】:2018-07-06 00:34:59
【问题描述】:

我是 android 新手,但拥有良好的 JavaFX 体验。我正在尝试创建一个可以重复使用的 自定义视图,但很难找出正确的方法。

在 javafx 中,我可以通过以下方式实现:创建一个单独的 fxml 文件,定义自定义视图的布局,然后创建一个链接到 fxml 文件的控制器类,在该类中,我有一种检索控制器数据模型并使用它来填充标签等的方法。

我想要的自定义视图是

  • 受限布局
    • TextView(约束到右锚点)
    • 圆形 TextView(约束到左锚点)

在android中执行此操作的最佳方法是什么?另外,是否可以使用 RecyclerView 来实现这一点?如果是,我如何为每个项目使用自定义视图并设置其数据?

【问题讨论】:

  • 这个问题很广泛。以 recyclerview 的任何示例并进行锻炼。在 main.xml 中创建一个 recyclerview,以及一个带有项目视图的单独文件。您的项目视图中有 3 个视图 - 带边距的白色背景(线性布局?)、右 textView 和左 textview。左侧的 textview 应该在您的 drawables 文件夹中定义了 android:background="drawable/round_shape" 和 round_shape.xml。一切都在 3 个 xml 文件中完成,recyclerview 的 main.xml、item.xml、round_background.xml。然后,recyclerview 适配器将 textviews 与您的数组绑定,并进行 recyclerview 初始化。
  • @EugeneKartoyev 谢谢,我现在就试试。如果您可以在答案中形成此评论。另外,我将如何将项目模型传递给项目视图。适配器如何知道要在模型中的哪个字段设置文本的文本视图。该模型将包含两个应该绑定到左右 TextViews 的字符串。 PS。啊,我想我应该为此创建一个自定义适配器类,因为我看到 [here] (stackoverflow.com/a/26748274/6918257)

标签: java android


【解决方案1】:

这个问题很广泛。您可能需要对创建视图进行更多研究

  1. 在main.xml中创建recyclerview,
  2. 带有项目视图的单独文件。
    • 您的项目视图中有 3 个视图 - 带边距的白色背景(线性布局?)、右侧 textView 和左侧 textview。
    • 左侧的 textview 应该在您的 drawables 文件夹中定义了 android:background="drawable/round_shape" 和 round_shape.xml。一切都在 3 个 xml 文件中完成,recyclerview 的 main.xml、item.xml、round_background.xml。然后,recyclerview 适配器将 textviews 与您的数组绑定,并进行 recyclerview 初始化

典型的 RV 适配器

 public class MyRV extends RecyclerView.Adapter<MyRV.ViewHolder> {

private List<MyModelItemWith2Strings> mDataSet; // You may need to setup an array, 
                   // with 2 String objects - for the right and left textviews


// Use an array of class with 2 elements rather than <String>, e.g. List<MyModelItemWith2Strings>
// pass your model here
 // this setData will be used to provide the contents for the textviews
void setData(List< /* set your 2 string class here*/ > dataSet) {
    mDataSet = dataSet;
}

static class ViewHolder extends RecyclerView.ViewHolder {

   // Here you bind item TV's
   // first you declare textviews that you will use to fill with data
   // Add any other item views you will need to fill in

    public TextView tv;
    public TextView tv2;

    public ViewHolder(LinearLayout v) {
        super(v);

        // Bind itemview views here. Put R.id.tv from your itemview.xml

        tv = v.findViewById(R.id.....);
         tv2 = v...
    }
}

// Add your itemview layout here
@Override
public MyRV.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
        .inflate(/***R.layout.item_view***/, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}


@Override
public void onBindViewHolder( MyRV.ViewHolder h, int position) {

    // get content from your model (the above list) and fill in the the itemview textviews

    String a= mDataSet.get(position).getItem1();
    String b = mDataSet.get(position). getItem2();
    ...

    h.tv.setText(a);
    // set clickers if you want to. The clicker class is below.
    h.tv.setOnClickListener(new Click(position));

    h.tv2.setText(...)
}

// This is obligatory to pass for your RV to initialize. It won't work if you don' t tell Android how to count your array soze
@Override
public int getItemCount() {
    return mDataSet.size();
}

// These are my implementation of clickers. I prefer to put them in the nested class of the adapter.
private class Click  implements OnClickListener {
    private int pos;
    Click(int position) {
        pos = position;
    }

    @Override
    public void onClick(View p1) {
        // get data from your array on click
        mDataSet.get(pos);
        // Use pos as position on the array, mData.get(pos)
    }
    }
}

然后,在你的主类中设置一个 recyclerview

   RecyclerView rv = (RecyclerView) findViewById(R.id.rv_In_Main_Xml);

    // just additional tunings. 
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(context)); // <- context = this, if you are in the Main activity

然后设置适配器

MyRV rva = new MyRV();
rva.setData(myArray_with_2_string_objects_to_fill_tvs);
rv.setAdaptor(rva);

然后您的回收站视图会充满数据

【讨论】:

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