0

I'm new to android, but have a good JavaFX experience. I'm trying to create a custom view that i can reuse, but having a hard time figuring out the correct way to do it.

In javafx i could achieve this by: Creating a separate fxml file defining the layout of the custom view, then create a controller class linked to the fxml file, in that class, i'd have a method to retrieve the data model of the controller and use it to fill in the labels, etc.

The custom view i want would be

  • Constrained Layout
    • TextView (constrained to right anchor)
    • Round TextView (constrained to left anchor)

enter image description here What is the best way to do this in android? Also, Is it possible to achieve this with a RecyclerView? If yes, how can i use a custom view for each item and set its data?

DarkMental
  • 482
  • 7
  • 26
  • The question is broad. Take any example of recyclerview and do exercise. Create a recyclerview in the main.xml, and a separate file with an item view. You have 3 views in your item view - white background with margins (linearlayout?), right textView, and left textview. The left textview should have android:background="drawable/round_shape" and round_shape.xml defined in your drawables folder. Everything is done in 3 xml files, main.xml for recyclerview, item.xml, round_background.xml. Then, the recyclerview adapter to bind the textviews with your array, and recyclerview initialization. – Eugene Kartoyev Jul 06 '18 at 00:48
  • @EugeneKartoyev Thank you, i will try that now. If you could form this comment in an answer. Also how would i go about passing the item model to the item view. How does the adapter know what textview to set the text of with what field in the model. The model would contain, two strings which should be bound to the right and left TextViews. PS. Ah i guess i should make a custom adapter class for this as i see [here] (https://stackoverflow.com/a/26748274/6918257) – DarkMental Jul 06 '18 at 00:55

1 Answers1

1

The question is broad. You may need additional research on creating views

  1. Create a recyclerview in the main.xml,
  2. a separate file with an item view.
    • You have 3 views in your item view - white background with margins (linearlayout?), right textView, and left textview.
    • The left textview should have android:background="drawable/round_shape" and round_shape.xml defined in your drawables folder. Everything is done in 3 xml files, main.xml for recyclerview, item.xml, round_background.xml. Then, the recyclerview adapter to bind the textviews with your array, and recyclerview initialization

A typical RV adaptor

 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)
    }
    }
}

Then, in your main class set a 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

Then set the adapter

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

And your recycler view gets filled with data

Eugene Kartoyev
  • 501
  • 4
  • 11