CardView in the ListView - java

I'm trying to load views with image and text by using CardView in the list. However, there is a divider between the cards.
This is my output:
I'm trying to do like in this video, no divider lines.
Here's my XML code.
activity_cat_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/lvCat"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
item_cat
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FEFEFE"
android:layout_margin="8dp"
app:cardCornerRadius="4dp"
card_view:cardCornerRadius="4dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
card_view:cardElevation="8dp">
<!-- Main Content View -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ivCat"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/tvCatName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ivCat"
android:text="Name"
android:maxLines="3"
android:padding="8dp"
android:textColor="#222"
android:textStyle="bold"
android:textSize="22dp" />
<TextView
android:id="#+id/tvDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvCatName"
android:text="Description"
android:maxLines="3"
android:padding="8dp"
android:textColor="#666"
android:textSize="14dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
edit 1: add CatAdapter.java
public class CatAdapter extends ArrayAdapter<Cat> {
public CatAdapter(Context context, ArrayList<Cat> cats) {
super(context,0,cats);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get data item from this position
Cat cats = getItem(position);
//check if existing view is being reused, otherwise inflate the views
if (convertView == null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_cat, parent, false);
//lookup view for data population
TextView tvName = (TextView)convertView.findViewById(R.id.tvCatName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
ImageView ivCat = (ImageView)convertView.findViewById(R.id.ivCat);
//populate into template view from data source
tvName.setText(cats.getName());
tvDescription.setText(cats.getDescription());
Glide.with(getContext()).load(cats.getImages()).into(ivCat);
return convertView;
}
}

You can remove divider with xml :
<ListView
android:id="#+id/lvCat"
android:dividerHeight="0dp"
android:divider="#null"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

Related

Listview not scrolling inside custom listview

I have a custom listview, inside of which there is another one. And the one that is nested will not scroll. Perhaps you need to register something in the parent setOnTouchListener? I don't have anything like that in getView.
image
my layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
tools:context=".invoice.ListPruducts">
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:divider="#android:color/transparent"
android:dividerHeight="10.0sp"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp"
android:stackFromBottom="true"
app:layout_constraintBottom_toTopOf="#+id/materialCardView2"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="10dp" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView2"
android:background="#color/zxing_transparent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/CustomCardViewStyle"
android:layout_marginTop="10dp"
app:cardElevation="10dp"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="#+id/productName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:backgroundTint="#color/yellow"
android:enabled="false"
android:hint="Наименование"
android:textColor="#color/black"
android:textColorHint="#color/black"
android:textSize="24sp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/productName"
android:layout_marginHorizontal="5dp"
android:layout_marginTop="5dp"
android:baselineAligned="false"
android:orientation="horizontal">
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="#+id/search_barCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="#color/green"
android:ems="15"
android:focusable="true"
android:hint="Считайте ш/к"
android:textColorHint="#color/DefGray"
android:inputType="number"
android:maxLength="15"
android:textSize="18sp"
android:singleLine="true" />
<com.google.android.material.textfield.MaterialAutoCompleteTextView
android:id="#+id/coutOfProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.1"
android:backgroundTint="#color/green"
android:hint="Кол-во: "
android:inputType="numberSigned|numberDecimal"
android:maxLength="13"
android:singleLine="true"
android:textColor="#color/black"
android:textColorHint="#color/DefGray"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
`
my getView
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
GoodsCreate_SQL myItemFromList = getItem(position);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResourse, parent, false);
TextView tvMark = convertView.findViewById(R.id.tvMark);
CheckBox cb = convertView.findViewById(R.id.checkBox2);
tvMark.setText(myItemFromList.Mark);
if (myItemFromList.myMark.equals("1")){
cb.setChecked(true);
}
return convertView;
}
Tried setOnTouchListener to write view.getParent().requestDisallowInterceptTouchEvent(true);

OnItemClick isn't working on a ListView item

I have a custom layout for the list's cells where you have a checkbox and 3 text elements. I found on Stackoverflow that I should add android:descendantFocusability = "blocksDescendants" on my LinearLayout where the list is but it doesn't change anything.
Here is my XML for the page with the list :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cl_fragment_detail_apero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
tools:context=".ui.home.AperoDetailFragment"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/date_apero"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:ems="10"
android:gravity="right"
android:textSize="18sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DDDDDD" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txtView_shopping_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:paddingHorizontal="10dp"
android:text="#string/shopping_list_title" />
<Button
android:id="#+id/btn_add_ingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/colorAccent"
android:clickable="true"
android:focusable="true"
android:padding="15dp"
android:src="#android:drawable/ic_menu_add"
android:text="#string/btn_add_ingredient"
android:textColor="#FFFFFF" />
</LinearLayout>
<ListView
android:id="#+id/list_ingredient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" />
</LinearLayout>
then in my Java code I set the listener like this :
#Override
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
final TextView name = (TextView) root.findViewById(R.id.name_apero);
name.setText(detailApero.name);
TextView date = (TextView) root.findViewById(R.id.date_apero);
date.setText(detailApero.date);
// some code that aren't useful right now
//-------------- List of ingredients for the detailed apero
final ListView list_ingredient = (ListView) root.findViewById(R.id.list_ingredient);
LaperoDatabase db = Room.databaseBuilder(root.getContext(),
LaperoDatabase.class, "lapero_db").allowMainThreadQueries().build();
IngredientDao dbIngredient = db.getIngredientDao();
//#TODO control if we get the right ingredients
ArrayList<Ingredient> ingredient_of_this_apero = (ArrayList<Ingredient>) dbIngredient.getIngredientsFromApero(detailApero.aid);
IngredientAdapter adapter = new IngredientAdapter(root.getContext(), R.layout.ingredient_cell_layout, ingredient_of_this_apero);
list_ingredient.setAdapter(adapter);
list_ingredient.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Ingredient selected_ingredient = (Ingredient) adapter.getItemAtPosition(position);
Log.e("test","okasodkdaso");
}
});
return root;
}
I also tried using a OnItemLongClickListener but it also does nothing. How can I put a listener for the list items ?

Android ListView background color masks onClick ripple effect

I have a custom ListView where each element has an icon and 3 TextViews. I am able to change the background to white but now I don't see the dynamic effect when I click on the text. I mean the gray color on touch (ripple effect).
this is the MainActivity
ListView mListView = findViewById(R.id.list_layout);
names = new ArrayList<>();
macs = new ArrayList<>();
adapter= new CompleteListAdapter(this, names, macs);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Method in my CompleteListAdapter which extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(mContext).inflate(R.layout.two_line_icon, parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.text1);
TextView text2 = (TextView) rowView.findViewById(R.id.text2);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
text1.setText(names.get(position));
text2.setText(macs.get(position));
return rowView;
}
This is part of the main_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.example.myapp.MainActivity">
<ListView
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="400dp"></ListView>
</LinearLayout>
And this is the single entry of the ListView: two_line_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<ImageView android:id="#+id/icon"
android:foregroundTint="#android:color/transparent"
android:foregroundTintMode="add"
android:scaleType="fitStart"
android:src="#drawable/img_logo"
android:tintMode="add"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Texto 1"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Tet2"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="text3"/>
</LinearLayout>
</LinearLayout>

How add framelayout in first group listview(ExpandbleListView)?

I'm trying to add to the first group of the new layout. But the disclosure of the groups, the new layout appears in the other groups. I want a new layout was added only in the first group expandblelistview. But my clumsy code adds it in all groups, and these groups do not clickable.
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.mylistview, parent, false);
}
if(groupPosition == 0)
{
LinearLayout fl = (LinearLayout)convertView.findViewById(R.id.mylistview);
View fl2 = inflater.inflate(R.layout.infoday, fl, true);
//fl.addView(fl2);
}
TextView textChild = (TextView)convertView.findViewById(R.id.textView22);
Group group = (Group)getGroup(groupPosition);
textChild.setText(group.string);
return convertView;
}
My list
<?xml version="1.0" encoding="utf-8"?><FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/expListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
</ExpandableListView>
My group in list
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="#+id/mylistview">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView22"
android:textSize="14sp"
android:layout_marginTop="20dp"
android:layout_marginLeft="35dp" />
My new layout, which i want to add
<?xml version="1.0" encoding="utf-8"?><FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/frameinfoday"
android:paddingBottom="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="25dp"
android:id="#+id/imageView2"
android:background="#drawable/day" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="СУББОТА"
android:id="#+id/textView25"
android:textColor="#ffffffff"
android:layout_marginLeft="25dp"
android:layout_marginTop="4dp"
android:textIsSelectable="true"
android:textSize="11sp" />
View headerView = View.inflate(this, R.layout.infoday, null);
listView.addHeaderView(headerView);
This is work for me. I add my layout in header listview!
More information: add to header and footer in listview

How do I make an XML layout for a ListView row where there is a TextView left-aligned and a TextView right-aligned?

Basically what I am looking for is my rows in my ListView to look something like this:
Title: Harry Potter
Hardcover: Yes
Own: No
Country: United States
Can someone post the XML layout of one of these rows? I can't seem to get it right with my code.
Here is what I have that just is plain wrong and not working:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtKey"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:textSize="18dip"
android:layout_marginLeft="8dip"
android:textStyle="bold"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:text="test"
/>
<TextView
android:id="#+id/txtValue"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:textSize="18dip"
android:layout_marginRight="8dip"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:text="test2"
/>
</TableRow>
</TableLayout>
</LinearLayout>
Got it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtKey"
android:text="Some text"
android:textSize="18dip"
android:textStyle="bold"
android:layout_marginLeft="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1.0"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
/>
<TextView
android:id="#+id/txtValue"
android:text="Some text"
android:textSize="18dip"
android:layout_marginRight="8dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1.0"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
/>
</TableRow>
</TableLayout>
</LinearLayout>
res/layout/listitem.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="match_parent">
<TextView
android:id="#+id/textLeft"
android:layout_weight="1"
android:gravity="left"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textRight"
android:layout_weight="1"
android:gravity="right"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
then in your activity:
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new BaseAdapter() {
final String[][] keysAndValues = {
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
};
#Override
public int getCount() {
return keysAndValues.length;
}
#Override
public String[] getItem(int position) {
return keysAndValues[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView[] foundTextFields;
if(convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem, parent, false);
foundTextFields = new TextView[] {
(TextView) convertView.findViewById(R.id.textLeft),
(TextView) convertView.findViewById(R.id.textRight)
};
convertView.setTag(foundTextFields);
}
else {
foundTextFields = (TextView[]) convertView.getTag();
}
String[] item = getItem(position);
foundTextFields[0].setText(item[0]);
foundTextFields[1].setText(item[1]);
return convertView;
}
});

Categories

Resources