Reputation: 578
I have a RecyclerView and two types of views(headers and item -> these are the same class objects, but contains different data). I want a header to be visible but it should take no space (something like in FrameLayout). Item below header should be drawn over this header (in its place, but both should be visible). Items work just like in LinearLayout.
I was thinking about writing my own custom LayoutManager. Is it a good idea, or is there a better solution to my problem? Can you point me at any direction? Eventually what should I concider writing this LayoutManager?
I forgot to mention that I cannot use any external library. I have to do it on my own.
Upvotes: 0
Views: 916
Reputation: 3835
Before giving you answer just look at the Definitions
VIEW.INVISIBLE:
This view is invisible, but it still takes up space for layout purposes.
VIEW.GONE:
This view is invisible, and it doesn't take any space for layout purposes.
For better understanding look at this screen shots https://stackoverflow.com/a/23211042/4741746
Now come to the point You don't need two types of views just one type of view is enough means item view. What you need is make your costume adapter in that adapter create view for header at left side and item at right side like this let consider this as row_layout.xml
<?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="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="@+id/tv_receipt_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:padding="3dp"
android:text="Header"
android:textSize="@dimen/title_text_font_size" />
<TextView
android:id="@+id/tv_receipt_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="end"
android:padding="3dp"
android:text="item"
android:textSize="@dimen/title_text_font_size" />
</LinearLayout>
DataAdpater.java file is like
class DataAdapter extends BaseAdapter {
ArrayList<UserReadOnlyData> receiptFields;
Context context;
public DataAdapter(Context context, ArrayList<UserReadOnlyData> receiptFields) {
this.receiptFields = receiptFields;
this.context = context;
}
@Override
public int getCount() {
return this.receiptFields.size();
}
@Override
public UserReadOnlyData getItem(int position) {
return receiptFields.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint({ "ViewHolder", "InflateParams" })
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater receiptRowViewInflator = LayoutInflater
.from(context);
convertView = receiptRowViewInflator.inflate(
R.layout.row_layout, null);
TextView header = (TextView) convertView
.findViewById(R.id.tv_receipt_label);
TextView item = (TextView) convertView
.findViewById(R.id.tv_receipt_value);
UserReadOnlyData receiptConfig = receiptFields.get(position);
if(null!=receiptConfig.header&&receiptConfig.header.length()>0){
header.setText((null!=receiptConfig.header)?receiptConfig.header :"");
}else {
header.setVisibility(View.VISIBLE);
}
item.setText((null!=receiptConfig.item)?receiptConfig.item :"");
return convertView;
}
}
public class UserReadOnlyData{
String header;
String item;
public UserReadOnlyData(String lable,String value){
this.header =lable;
this.item =value;
}
}
Than set your adpter like
ArrayList<UserReadOnlyData> receiptFields= getYourValues();
DataAdapter adapter = new DataAdapter (mContext, receiptFields);
listView.setAdapter(adapter);
Upvotes: 1
Reputation: 11
Every itemView contains a headerView, but only the first itemView's headerView is visible, leaving others invisible.
It's easy to implement, while you add a useless view on every itemView, which is bad for performance.
Since you are using RecyclerView, not ListView. I recommended RecyclerView.ItemDecoration.
Upvotes: 0
Reputation: 5196
Use rootView.setVisibility(View.INVISIBLE);
on the root element of your item in the onBind()
method of your ViewHolder
Upvotes: 0