I'm having a problem with setting the layout in RecyclerView.
here is an layout xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/dummy_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:background="#ffffff" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Button"
android:textAllCaps="false" />
</LinearLayout>
here is my MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
List<String> list = new ArrayList<>();
for (int i = 0; i <= 25; i++) {
list.add("item " + i);
}
DummyRVAdapter adapter = new DummyRVAdapter(MainActivity.this, list);
recyclerView.setAdapter(adapter);
}
Code for Adapter :
public class DummyRVAdapter extends RecyclerView.Adapter<DummyRVAdapter.Holder> {
private Context mContext;
private List<String> mList;
public DummyRVAdapter(Context context, List<String> list) {
this.mContext = context;
this.mList = list;
}
@Override
public DummyRVAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(mContext, R.layout.item_dummy_layout, null);
//View view =LayoutInflater.from(mContext).inflate(R.layout.item_dummy_layout, null);
return new Holder(view);
}
@Override
public void onBindViewHolder(DummyRVAdapter.Holder holder, int position) {
holder.textView.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView textView;
public Holder(View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.dummy_text_view);
}
}
}
Screenshot of my output.
In my layout XML file, I declare the size for textView as 50% and Button as 30% ,but still, my recyclerView not showing in a correct manner it is not showing in a correct manner.
This layout works fine in Listview.
I don't know where is the problem in my code. I set the Layout manager and adapter to my RecyclerView in the correct manner.
Edit: Please see the ScreenShot it shows that TextView is not allocating 50% of the screen, that's the problem.
It I create complex Layout for Recyclerview its alignment is not in mentioned format.
but that works in Listview
Can anyone help me on this?
Thanks in advance.