在Android中用作嵌套视图或RecyclerView Item视图的自定义视图

时间:2018-07-06 00:34:59

标签: java android

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

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

我想要的自定义视图将是

  • 布局受限
    • TextView(仅限于右锚点)
    • 圆形TextView(仅限于左锚点)

enter image description here 什么是在android中做到这一点的最佳方法?另外,是否可以使用 RecyclerView 实现此目标?如果是,我如何为每个项目使用自定义视图并设置其数据?

1 个答案:

答案 0 :(得分:1)

问题很广泛。您可能需要进行其他有关创建视图的研究

  1. 在main.xml中创建一个recyclerview,
  2. 具有项目视图的单独文件。
    • 您在项目视图中有3个视图-带边距的白色背景(linearlayout?),右侧textView和左侧textview。
    • 左侧的textview应该在您的drawables文件夹中定义了android:background =“ drawable / round_shape”和round_shape.xml。一切都在3个xml文件中完成:recyclerview的main.xml,item.xml,round_background.xml。然后,使用recyclerview适配器将文本视图与您的数组绑定,并进行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);

您的回收者视图中充满了数据