ExpandableListView Change Indicator State - java

I have an ExpandableListView setup working very properly and as I wanted but with a minor problem I want the group indicator (the arrow) to expand and collapse when I open or close the group. This function is although partially working but not fully correct.
The problem is that when I expand lets say group1 the indicator of group2 automatically gets invisible (Image at the end of post). and if I continue to expsnd group3 then the indicator of group4 hides. at the end only the terminal groups i.e ending groups have the indicator, the indicators of all other groups are invisible. I tried debugging this very hard but could find any reason as to why this is happening. Maybe you all can take a look!
This is the activity_main.xml (Yes I am Using Android Navigation Drawer Here)
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ExpandableListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:groupIndicator="#null"
android:divider="#F3F3F3"
android:padding="1dp"
android:dividerHeight="1dp"
android:background="#FFFFFF" />
</android.support.v4.widget.DrawerLayout>
drawer_group_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/group_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="24sp"
android:gravity="left"
android:minHeight="30dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#616161"
android:text="Hello"
android:paddingRight="40dp" />
<ImageView
android:id="#+id/indicator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:src="#drawable/ic_action_expand" />
</RelativeLayout>
drawer_list_item
<?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" >
<ImageView
android:id="#+id/store_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:scaleType="fitXY" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:textColor="#616161"
android:textSize="24sp" />
</LinearLayout>
getGroupView from the adapter
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.drawer_group_item, null);
holder.imageView = (ImageView) convertView.findViewById(R.id.indicator);
holder.textView = (TextView) convertView.findViewById(R.id.group_title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(parentItems.get(groupPosition));
if(groupPosition == 0 || groupPosition == 5){
holder.imageView.setVisibility(View.GONE);
}
if(isExpanded){
holder.imageView.setImageResource(R.drawable.ic_action_collapse);
}
if(!isExpanded){
holder.imageView.setImageResource(isExpanded?R.drawable.ic_action_collapse:R.drawable.ic_action_expand);
}
return convertView;
}
Image: http://imgur.com/JzsVdQz
Image order is right to left (sorry about that)
EDIT: I also want some categories to not expand. like if there is a group which doesnot have subitems I want to hide the indicator for that group. I am using this code currently
if(groupPosition == 0 || groupPosition == 5){
holder.imageView.setVisibility(View.GONE);
}
but how do i hide the indicator in the statelist xml and setIndicatorBounds method (as stated by user3664308)

You should instead create a drawable with different states for an expanded and collapsed group like:
drawable/group_indicator.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_expanded="true"
android:drawable="#drawable/ic_action_expand" />
<item
android:drawable="#drawable/ic_action_collapse" />
</selector>
And then set the indicator:
mExpList.setIndicator(R.drawable.group_indicator.xml);
Then adjust the indicators position by setting its bounds:
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
mListView.setIndicatorBounds(myLeft, myRight);
} else {
mListView.setIndicatorBoundsRelative(myLeft, myRight);
}

Related

CardView in the ListView

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>

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

Android left and right alignment of listview

I want to align the listview left and right inside a RelativeLayout as done in IM apps.
My Problem is that things are getting random. Sometimes when m.getme() == 0 then its msg_row_he.xml layout loaded but sometimes its msg_row_me.xml loaded. And the align of listview's are changing at every scroll. Can anyone figure out what is going on ?
msgAdapter.java (getView Method)
public View getView(int position, View convertView, ViewGroup parent) {
// getting data for the row
MSList m = listItems.get(position);
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
if(m.getme() == 0){
convertView = inflater.inflate(R.layout.msg_row_he, null);
}else{
convertView = inflater.inflate(R.layout.msg_row_me, null);
}
}
ImageView pp = (ImageView) convertView
.findViewById(R.id.ind_pp);
TextView msgcon = (TextView) convertView.findViewById(R.id.ind_msgcon);
TextView msgtime = (TextView) convertView.findViewById(R.id.ind_msgtime);
// thumbnail image
new ImageLoadTask(m.getppUrl(),pp).execute();
// content
msgcon.setText(m.getmsgcon());
// time
msgtime.setText(String.valueOf(m.getmsgtime()));
convertView.setTag(m);
return convertView;
}
msg_row_me.xml (right aligned)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_row_selector"
android:padding="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<!-- Content -->
<TextView
android:id="#+id/ind_msgcon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fbfbfb"
android:textColor="#color/msgcon"
android:textSize="#dimen/msgcon" />
<!-- Thumbnail Image -->
<ImageView
android:id="#+id/ind_pp"
android:layout_width="45dp"
android:layout_height="45dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<!-- Message Time -->
<TextView
android:id="#+id/ind_msgtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/msgtime"
android:textSize="#dimen/msgtime" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
msg_row_he.xml (left aligned)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_row_selector"
android:padding="8dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<!-- Thumbnail Image -->
<ImageView
android:id="#+id/ind_pp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="8dp" />
<!-- Content -->
<TextView
android:id="#+id/ind_msgcon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fbfbfb"
android:textColor="#color/msgcon"
android:textSize="#dimen/msgcon" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<!-- Message Time -->
<TextView
android:id="#+id/ind_msgtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/msgtime"
android:textSize="#dimen/msgtime" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
You have to override getItemViewType(int position) and getViewTypeCount()
Lets say you have two types of layout (left and right) then
#Override
public int getViewTypeCount() {
return 2;
}
and
#Override
public int getItemViewType(int position) {
return listItems.get(position).getme(); // hoping this returns value as 0 and 1
}
In your getView method change this condition
if(m.getme() == 0)
to
if(getItemViewType(position)==0)
Here you are have different layout for the position then you have to inflate in everytime
Change
if (convertView == null){
if(m.getme() == 0){
convertView = inflater.inflate(R.layout.msg_row_he, null);
}else{
convertView = inflater.inflate(R.layout.msg_row_me, null);
} }
To
if(m.getme() == 0){
convertView = inflater.inflate(R.layout.msg_row_he, null);
}else{
convertView = inflater.inflate(R.layout.msg_row_me, null);
}

Android problems setting the background of a view in a expandable list view

I'm trying to set the color of one specific view with the parent of the expandable list, however when I do I'm finding that colors in the child views are being affected as well which is strange.
Changing the view to original view from white to black seems to solve the issue and it only seems to arise on some phones, its really starting to drive me mad, but I can't find anybody thats run into the same issue.
Heres my custom class that extends BaseExpandableListAdapter.
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(SUM_HOST, parent, false);
}
View statView = (View)v.findViewById(R.id.statViewABCDE);
int caseTest = hostParents.get(groupPosition).getState();
switch (caseTest) {
case Constants.STATE_OK:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_up_state));
break;
case Constants.STATE_DOWN:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_down_state));
break;
case Constants.STATE_UNKNOWN:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_unreachable_state));
break;
}
return v;
}
Here is my XML file for the parent:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp" >
<LinearLayout
android:id="#+id/sumHostLay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/defViewMargin"
android:layout_marginRight="#dimen/defViewMargin"
android:layout_marginTop="#dimen/defViewMargin"
android:background="#drawable/rounded_corners_background"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="#dimen/defViewPadding" >
<View
android:id="#+id/statViewABCDE"
android:layout_width="3dip"
android:layout_height="fill_parent"
android:background="#android:color/black" />
<TextView
android:id="#+id/sumHostTitleAndOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="#string/hostrow_hostname"
android:textSize="13sp" />
</LinearLayout>
Here is my XML for the child.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="0dp" >
<LinearLayout
android:id="#+id/asdasd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="#dimen/defViewMargin"
android:orientation="vertical" >
<View
android:id="#+id/line1"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_margin="3dp"
android:background="#android:color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<View
android:id="#+id/line4"
android:layout_width="20dp"
android:layout_height="1dp"
android:background="#android:color/white" />
</LinearLayout>
In theory I am just trying to set the view in the parent XML i.e statViewABCDE however I am finding that the background colors are changing in the child view aswell. Specifically "line1" and "line4" are changing.
EDIT:
Setting the lines to be different colors (i.e Black for one set and white for the others, seems to have worked, although I'm not sure why.)
Found this issue while testing on a phone running Android 4.1.2.
I can't find any conclusive evidence of whats going on here.
To get around this issue I set the color of the background in java of each view, this seems to have resolved the issue.

Android List onListItemClick not working

I've got a new List of things that needs to be clicked but this one isn't working. onListItemClick is never called. I have another one in my app that has been working as expected and I can't figure out what the difference is. I've seen the people saying to change focusable but I've tried that a bunch of different ways with no effect. So here's some code.
Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
v_ProjectInvestigatorSiteContact project = (v_ProjectInvestigatorSiteContact) this.getListAdapter().getItem(
position);
Intent myIntent = new Intent(this, Details.class);
myIntent.putExtra(res.getString(R.string.project), project);
startActivity(myIntent);
}// onListItemClick
Not Working:
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// get the item that was clicked
final v_SitePeople vSitePeople = (v_SitePeople) this.getListAdapter().getItem(
position);
AlertDialog.Builder builder = new AlertDialog.Builder(Share.this);
builder.setTitle(res.getString(R.string.forgot_password_check_dialog_title))
.setMessage(res.getString(R.string.share_check_dialog_text))
.setPositiveButton(res.getString(R.string.send), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
sendShareEmail(vSitePeople);
}
}).setNegativeButton(res.getString(R.string.cancel), new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
// cancelled, so do nothing
}
});
AlertDialog msgBox = builder.create();
msgBox.show();
}// onListItemClick
Working 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"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/app_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/search_gradient" >
<ImageView
android:id="#+id/searchBoxIcon"
android:layout_width="38dp"
android:layout_height="38dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop"
android:src="#drawable/action_search" >
</ImageView>
<EditText
android:id="#+id/searchBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="#+id/searchBoxIcon"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="#+id/searchBoxIcon"
android:background="#drawable/search_box"
android:hint="#string/search_hint"
android:inputType="text"
android:maxLines="1"
android:minHeight="30sp"
android:paddingBottom="2dp"
android:paddingLeft="25sp"
android:paddingTop="2dp"
android:textColor="#ff000000" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Not Working 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"
android:background="#ffffffff"
android:orientation="vertical" >
<!-- dummy item to prevent edittext from gaining focus on activity start -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/title_background" >
<ImageView
android:id="#+id/logo"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_logo" >
</ImageView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/logo"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingLeft="50dp"
android:paddingRight="60dp"
android:paddingTop="5dp"
android:text="#string/share_header"
android:textColor="#ffffffff"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/divider_gray"
android:cacheColorHint="#00000000"
android:divider="#color/divider_gray"
android:dividerHeight="1dp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" />
<TextView
android:id="#+id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/loading"
android:textColor="#color/loading_gray"
android:textSize="20sp" />
</LinearLayout>
Here is more on how the broken one works, just in case you want more code.
Not Working Row XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_weight="1"
android:baselineAligned="false"
android:orientation="vertical"
android:padding="6dp"
android:background="#ffffffff"
android:layout_margin="10dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/moreInfo"
android:gravity="center_vertical"
android:minHeight="20sp"
android:textColor="#ff000000"
android:textStyle="bold" />
</RelativeLayout>
Not Working View Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.share_row, null);
convertView.setTag(holder);
}// if
else
{
holder = (ViewHolder) convertView.getTag();
}
v_SitePeople i = items.get(position);
if (i != null)
{
TextView topText = (TextView) convertView.findViewById(R.id.toptext);
topText.setGravity(Gravity.CENTER_VERTICAL);
topText.setMinHeight(40);
if (topText != null)
{
if (i.SitePerson != null)
{
if (i.PersonTitle != null)
{
topText.setText(String.format(i.SitePerson + ", " + i.PersonTitle));
}
else
{
topText.setText(i.SitePerson);
}
}// if has ProtocolNumber
else
{
if (i.Nickname != null)
{
topText.setText(i.Nickname);
}
}// if does not have ProtocolNumber
}// if
}// if
return convertView;
}// getView
Thank you so much for your help.
I figured it out. I never posted the relavant code, or I'm sure you guys would have found it for me.
My not working adapter had a isEnabled method copied from my functioning adapter that disabled items of type 0. But since my new list is all just one type everything was disabled.
Sorry for the trouble and thank you for your efforts.
You haven't shown where you initialise your list and adapter, but you need to make sure the onClickListner is set. Something like:
myListView.setOnItemClickListener(this);
edit: 'this' assuming initialization is done in the same class as the click listeners appear.
When you're using a ListView, you have mainly two layouts:
1)The layout where you have your listView
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="?attr/actionBarSize"
android:clickable="false"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:clipToPadding="false"
android:paddingLeft="#dimen/list_left_padding"
android:paddingRight="#dimen/list_right_padding"
android:paddingTop="#dimen/list_separator"
android:dividerHeight="#dimen/list_separator"
android:paddingBottom="#dimen/list_separator" />
</RelativeLayout>
2)The layout which contains each item for your ListView.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/white_responsive"
android:descendantFocusability="blocksDescendants"
>
<RelativeLayout
android:id="#+id/sampleItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:clipToPadding="false">
<com.devspark.robototextview.widget.RobotoTextView
android:id="#+id/textViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="5dp"
android:text="Sample Text"
android:textSize="17sp"
app:typeface="roboto_regular" />
</RelativeLayout>
</RelativeLayout>
If you search on StackOverFlow about this problem, you will got the following instructions to solve it:
1) Add
android:descendantFocusability="blocksDescendants"
to the root of your layout
2) Add
android:clickable="false"
android:focusable="false"
To your item.
You can use these two options separately. The first one have helped me. But, you need to pay attention on where you need to put these attributes in your XML File. Put them in your ITEM layout, not in your ListView Layout. I'm saying this, because I was using that solution, but i was putting on the wrong xml. So, it just pay attention and it will work very well.

Categories

Resources