Generating a listfragment on a tablet with selector arrow

Put your problem here if it does not fit any of the other categories.

Generating a listfragment on a tablet with selector arrow

Postby gooroo175 » Sun Mar 04, 2012 9:53 am

Hi,

I'm desperately trying to create a listfragment that looks similar to the ones used in the googlemail and standard mail app on tablets. For selected entries they have an arrow on the right pointing to the entry. The whole list has a divider line on the right, that drops a shawdow towards the list.

I tried to rebuild this with two means. First a black line-9-patch as a background image for the whole listview. For the arrow I added an ImageView on top of each list item making it float on the right. Everything works fine so far, although I don't know whether this is the most elegant way.

My problem ist the state dependency of the arrow. It should only be visible when the item is selected. I set the "duplicateState" property of the ImageView to duplicate the state of the image. And as a source I added a drawable that uses a selector. I tried all different combinations of states but the image does not show up... Any suggestions?!

This is my list item and my selector-drawable:
Code: Select all
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_marginRight="2dp"
        android1:src="@drawable/arrow_vis"
        android1:visibility="visible" >
    </ImageView>

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:text="Title"
        android:textSize="20dip"
        android:textStyle="bold" />

</merge>


Code: Select all
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true"   android:state_checked="true" android:drawable="@drawable/arrow_empty" />
    <item android:state_focused="false"  android:state_checked="true" android:drawable="@drawable/arrow" />

</selector>
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Top

Re: Generating a listfragment on a tablet with selector arro

Postby gooroo175 » Sun Mar 04, 2012 12:39 pm

Image

Just to show you. This is what I'm trying to create.
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Re: Generating a listfragment on a tablet with selector arro

Postby Phyll » Sun Mar 04, 2012 3:15 pm

Hi gooroo,

Just wondering if you had tried this one:

android:state_selected

State value for StateListDrawable, set when a view (or one of its parents) is currently selected. You may also want to make sure the item actually gets selected in the list.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Generating a listfragment on a tablet with selector arro

Postby gooroo175 » Sun Mar 04, 2012 6:25 pm

Thanks for your reply Phyll. I think i tried all possible combinations of states (for hours ;). The code I posted above is to some degree from the original listBackground-Style. Before I tried out the following code.

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="false">
<item android:state_pressed="true"
    android:drawable="@drawable/arrow" />
<item android:state_focused="true"
    android:drawable="@drawable/arrow" />
<item android:state_selected="true"
    android:drawable="@drawable/arrow" />
<item
    android:drawable="@drawable/arrow_empty" />
</selector>


This works for "presses"... The arrow appears while the row is pressed. But after releasing it the item has no more arrow although it is selected and has a blue background. When I use the arrow as a standard configuration all arrows are visible all times (s. screenshot).

Any more suggestions?

Image
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Re: Generating a listfragment on a tablet with selector arro

Postby Phyll » Mon Mar 05, 2012 12:15 am

Hi gooroo,

If this is a listview and you have a selectedadapter of one kind or another, you could choose the imageview source during the getView method. Something like this:

Code: Select all
        ImageView iv = (ImageView)v.findViewById(R.id.border_image);

        if(selectedPos == position){
           iv.setImageResource(R.drawable.arrow);
            }//selected one
            else {
               iv.setImageResource(R.drawable.no_arrow);
                }


I don't know how useful it is but its another suggestion.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Generating a listfragment on a tablet with selector arro

Postby nuetronprotron » Mon Mar 05, 2012 10:02 am

Hi gooroo,
As Phyll stated above u can do it through custom listView and i think below and hope it is helpfull to you

Code: Select all
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_pressed="true" android:state_selected="true" android:drawable="@drawable/arrow" />
<item android:state_selected = "false"  android:drawable="@drawable/arrow_empty" />

</selector>
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Top

Re: Generating a listfragment on a tablet with selector arro

Postby gooroo175 » Mon Mar 05, 2012 10:54 pm

Thanks for the reply. The getView thing nearly works. A last problem I have is that I don't get proper access to the currently selected item in getView.

Code: Select all
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         LogNow.log(this,"Getting View"+listView.getSelectedItemPosition()+"=="+position);
         View v = super.getView(position, convertView, parent);
         ImageView iv = (ImageView) v.findViewById(R.id.selected_arrow);

         iv.setImageResource(R.drawable.arrow);
//         if (listView.getSelectedItemPosition() == position) {
//            iv.setImageResource(R.drawable.arrow);
//         } else {
//            iv.setImageResource(R.drawable.arrow_empty);
//         }
         return v;
      }


This is my code so far. Setting the image basically works, but listView.getSelectedItemPosition() always returns "-1". My Adapter inherits from CusorAdatper....

By the way do you have exlenation why the originally intended way does not work?
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Re: Generating a listfragment on a tablet with selector arro

Postby gooroo175 » Mon Mar 05, 2012 11:06 pm

Theres another misterious thing I'm wondering about.

Code: Select all
      adapter = new MyCursorAdapter(activity, activity.getDb().getItems());
      listView.setAdapter(adapter);
      listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      listView.setOnItemSelectedListener(new OnItemSelectedListener() {

         public void onItemSelected(AdapterView<?> arg0, View v, int pos,
               long row) {
            // if (lastSelectedView!=null)
            // v.findViewById(R.id.selected_arrow).setVisibility(View.GONE);
            // lastSelectedView = v;
            // v.findViewById(R.id.selected_arrow).setVisibility(View.VISIBLE);
            LogNow.log(this, "Item selected: "+v);
         }

         public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

         }
      });


There was another thing i tried using the onItemSelected-Listener. but these methodes are never called? I think I'm doing something terribly wrong without knowing what ;)
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Re: Generating a listfragment on a tablet with selector arro

Postby Phyll » Mon Mar 05, 2012 11:17 pm

Hi gooroo,

I have no explanation for why your selector doesn't work except to note that if the getselectedposition always returns -1 (doesn't get set) then that might have something to do with it. At least with the state_selected attribute.

I have used the list adapter many times but I have really never had a reason to extend it from a cursoradapter so I'm afraid I can't be of too much help. I guess we need to find out where that information is really being stored. I know in some of the list views I have made where the rows were made from layouts with more than one widget or something I had to capture the onclick and store the postion myself. It could be that something else is capturing the onclick and not passing it on to the list adapter.

Here's some code from one of my listadapters:

Code: Select all
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected

public CallListAdapter(Context context, int textViewResourceId,
                          List<String> objects) {
  super(context, textViewResourceId, objects);
}

public void setSelectedPosition(int pos){
  selectedPos = pos;
  // inform the view of this change
  notifyDataSetChanged();
}

public int getSelectedPosition(){
  return selectedPos;
}


If you figure out what is actually getting the on click you could implement something like that code. This could be the missing piece.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Generating a listfragment on a tablet with selector arro

Postby nuetronprotron » Tue Mar 06, 2012 5:24 am

Hi gooroo,
I was wondering why you are using a cursoradapter.Are u trying any db thing,because normally cursors are meant to handle the database things,so is it that what you are trying or are you checking with the cursoradapter rather than the normal adapter.
I think for the onItemselected thing try to override it and return the parameter position in it.did u tried using the selector thing for the arrow image.I hope this information is helpful to u.
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Re: Generating a listfragment on a tablet with selector arro

Postby gooroo175 » Tue Mar 06, 2012 11:17 pm

Hi neutronproton,

I am working with db-stuff indeed. This is why I need that cursor adapter.
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Re: Generating a listfragment on a tablet with selector arro

Postby gooroo175 » Tue Mar 06, 2012 11:21 pm

Yes! I finally found a solution. I went back to the roots an neglected all my personal CursorAdapter stuff I wrote so far. I reduced the whole code to just a SimpleCursorAdapter.

Code: Select all
      String[] columns = {DBAdapter.COLUMN_NAME};
      int[] fields = {R.id.title};
      adapter = new SimpleCursorAdapter(activity,R.layout.list_item,activity.getDb().getLists(),columns,fields,0);


And the arrow_vis.xml-selector is nothing else then:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="false">
<item android:state_activated="true"
    android:drawable="@drawable/arrow" />
<item
    android:drawable="@drawable/arrow_empty" />
</selector>


So the "activated" state is the key and works so far. Next I'm going to restore all the functionalty I lost through this step and hope that the rest will still work.

Thank you all for your help!
gooroo175
Freshman
Freshman
 
Posts: 7
Joined: Sun Mar 04, 2012 9:30 am

Re: Generating a listfragment on a tablet with selector arro

Postby Phyll » Tue Mar 06, 2012 11:31 pm

Great! gooroo,

Good luck with the rest.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests