Today to achieve the following public comment on the horizontal ListView two level list of clients, first look at the style.
This horizontal listview Two-level list is less common on mobile apps, but it should be known that flat-panel listings are more common. It may be because the flat screen is bigger and more content can be displayed.
Let's take a look at my implementation steps.
First customize a ListView, the code is as follows:
Copy Code code as follows:
public class Mylistview extends ListView implements Runnable {
private float mlastdowny = 0f;
private int mdistance = 0;
private int mstep = 10;
Private Boolean mpositive = false;
Public Mylistview (context context, AttributeSet Attrs) {
Super (context, attrs);
}
Public Mylistview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
}
Public Mylistview {
Super (context);
}
@Override
public boolean ontouchevent (Motionevent event) {
Switch (event.getaction ()) {
Case Motionevent.action_down:
if (Mlastdowny = 0f && mdistance = 0) {
Mlastdowny = Event.gety ();
return true;
}
Break
Case Motionevent.action_cancel:
Break
Case MOTIONEVENT.ACTION_UP:
if (mdistance!= 0) {
Mstep = 1;
Mpositive = (mdistance >= 0);
This.post (this);
return true;
}
Mlastdowny = 0f;
mdistance = 0;
Break
Case Motionevent.action_move:
if (Mlastdowny!= 0f) {
mdistance = (int) (mlastdowny-event.gety ());
if (mdistance < 0 && getfirstvisibleposition () = 0 && getchildat (0). GetTop () = 0) | | (mdistance > 0 && getlastvisibleposition () = = GetCount ()-1)) {
Mdistance/= 2;
Scrollto (0, mdistance);
return true;
}
}
mdistance = 0;
Break
}
Return Super.ontouchevent (event);
}
public void Run () {
Mdistance + = mdistance > 0? -mstep:mstep;
Scrollto (0, mdistance);
if ((mpositive && mdistance <= 0) | | (!mpositive && mdistance >= 0)) {
Scrollto (0, 0);
mdistance = 0;
Mlastdowny = 0f;
Return
}
Mstep + 1;
This.postdelayed (this, 10);
}
}
Then look at the layout of the XML:
Copy Code code as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<com.example.multilistview.mylistview
Android:id= "@+id/listview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
Android:choicemode= "Singlechoice"
Android:scrollbars= "None"
android:divider= "@drawable/listitem_divide"
Android:listselector= "#00000000"
Android:background= "#e4e3de"
>
</com.example.multilistview.MyListView>
<com.example.multilistview.mylistview
Android:id= "@+id/sublistview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_weight= "1"
Android:background= "#e4e3de"
>
</com.example.multilistview.MyListView>
</LinearLayout>
Two custom ListView landscape layouts, then the parent ListView adapter
Copy Code code as follows:
public class Myadapter extends Baseadapter {
Context context;
Layoutinflater Inflater;
String [] foods;
int Last_item;
int [] images;
private int selectedposition =-1;
Public Myadapter (Context context,string [] foods,int[] images) {
This.context = context;
This.foods = foods;
This.images = images;
Inflater=layoutinflater.from (context);
}
@Override
public int GetCount () {
TODO auto-generated Method Stub
return foods.length;
}
@Override
Public Object getitem (int position) {
TODO auto-generated Method Stub
return position;
}
@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
Viewholder holder = null;
if (convertview==null) {
Convertview = inflater.inflate (R.layout.mylist_item, NULL);
Holder = new Viewholder ();
Holder.textview = (TextView) Convertview.findviewbyid (R.id.textview);
Holder.imageview = (ImageView) Convertview.findviewbyid (R.id.imageview);
holder.layout= (LinearLayout) Convertview.findviewbyid (r.id.colorlayout);
Convertview.settag (holder);
}
else{
Holder= (Viewholder) Convertview.gettag ();
}
Set the check effect
if (selectedposition = = position)
{
Holder.textView.setTextColor (Color.Blue);
Holder.layout.setBackgroundColor (Color.ltgray);
} else {
Holder.textView.setTextColor (Color.White);
Holder.layout.setBackgroundColor (color.transparent);
}
Holder.textView.setText (Foods[position]);
Holder.textView.setTextColor (Color.Black);
Holder.imageView.setBackgroundResource (Images[position]);
return convertview;
}
public static Class viewholder{
Public TextView TextView;
Public ImageView ImageView;
public LinearLayout layout;
}
public void setselectedposition (int position) {
Selectedposition = position;
}
}
The corresponding item layout:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/colorlayout"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<imageview
Android:id= "@+id/imageview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "10dip"
android:layout_margintop= "5dip"
/>
<textview
Android:id= "@+id/textview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= ""
Android:textsize= "16dip"
android:layout_margintop= "8dip"
android:layout_marginleft= "8dip"
android:layout_marginbottom= "8dip"/>
<!--android:background= "@drawable/selector" Custom ListView style-->
</LinearLayout>
Then the child Adapter code:
Copy Code code as follows:
public class Subadapter extends Baseadapter {
Context context;
Layoutinflater Layoutinflater;
String[][] cities;
public int foodpoition;
Public Subadapter (context context, string[][] Cities,int position) {
This.context = context;
this.cities = cities;
Layoutinflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
This.foodpoition = position;
}
@Override
public int GetCount () {
TODO auto-generated Method Stub
return cities.length;
}
@Override
Public Object getitem (int position) {
TODO auto-generated Method Stub
return GetItem (position);
}
@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
Viewholder viewholder = null;
final int location=position;
if (Convertview = = null) {
Convertview = layoutinflater.inflate (R.layout.sublist_item, NULL);
Viewholder = new Viewholder ();
Viewholder.textview = (TextView) convertview
. Findviewbyid (R.id.textview1);
Convertview.settag (Viewholder);
} else {
Viewholder = (Viewholder) convertview.gettag ();
}
ViewHolder.textView.setText (Cities[foodpoition][position]);
ViewHolder.textView.setTextColor (Color.Black);
return convertview;
}
public static class Viewholder {
Public TextView TextView;
}
}
corresponding XML layout:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
>
<textview
Android:id= "@+id/textview1"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "AAAAA"
Android:textsize= "16dip"
android:layout_margintop= "10dip"
android:layout_marginleft= "8dip"
android:layout_marginbottom= "8dip"/>
</LinearLayout>
Finally, see the implementation code of the main activity
Copy Code code as follows:
public class Mainactivity extends activity {
Private Mylistview ListView;
Private Mylistview Sublistview;
Private Myadapter Myadapter;
Private Subadapter Subadapter;
String cities[][] = new string[][] {
New string[] {"All food", "This help Jiangsu and Zhejiang cuisine", "Sichuan cuisine, Cantonese cuisine, Hunan cuisine, Dongbei cuisine, Taiwanese cuisine, Xinjiang/halal, vegetarian dishes, hot pot, buffet, snack food, Japan, Korean cuisine, and so on.
"Southeast Asian Cuisine", "Western Food", "bread dessert", "other"},
New string[] {"All Leisure and entertainment", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Scenic spot/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All shopping", "comprehensive shopping mall", "Clothing shoe bag", "Sports outdoors", "Jewelry jewelry", "cosmetics", "Digital Home Appliances", "parent-child shopping", "Home building Materials"
, "Bookstore", "bookstore", "Glasses Shop", "Featured Bazaar", "more shopping place", "food tea Wine", "Supermarket/convenience store", "pharmacy"},
New string[] {"All Leisure and entertainment", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Scenic spot/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All", "Café", "Bar", "Teahouse", "KTV", "Amusement", "Park", "Attractions/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All", "Café", "Bar", "teahouse", "Cinema", "Amusement", "Park", "sights/outings", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All Hugh", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Scenic spot/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All leisure", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Attractions/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All Leisure Entertainment", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Attractions/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games"},
New string[] {"All Leisure and entertainment", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Scenic spot/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games", "More Entertainment"},
New string[] {"All leisure AAA", "Café", "Bar", "Teahouse", "KTV", "movie theater", "Amusement and entertainment", "Park", "Attractions/outing", "bathing", "foot massage", "culture and art",
"DIY Handmade Workshop", "Table Arena", "Desktop Games"},
};
String foods[] =new string []{"All channels", "food", "Leisure and entertainment", "shopping", "Hotel", "Beauty", "Sports Fitness", "marriage", "parent-child", "car", "Life Service"};
int images[] = new int[]{r.drawable.ic_category_0,r.drawable.ic_category_10,r.drawable.ic_category_30, R.drawable.ic_category_20
, R.drawable.ic_category_60,r.drawable.ic_category_50,r.drawable.ic_category_45,r.drawable.ic_category_50, R.drawable.ic_category_70,
R.DRAWABLE.IC_CATEGORY_65,R.DRAWABLE.IC_CATEGORY_80};
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
Myadapter=new Myadapter (Getapplicationcontext (), foods, images);
Listview.setadapter (Myadapter);
Selectdefult ();
Listview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1, int position,
Long Arg3) {
TODO auto-generated Method Stub
final int location=position;
Myadapter.setselectedposition (position);
Myadapter.notifydatasetinvalidated ();
Subadapter=new Subadapter (Getapplicationcontext (), cities, position);
Sublistview.setadapter (Subadapter);
Sublistview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1,
int position, long Arg3) {
TODO auto-generated Method Stub
Toast.maketext (Getapplicationcontext (), cities[location][position], Toast.length_short). Show ();
}
});
}
});
}
private void init () {
listview= (Mylistview) Findviewbyid (R.id.listview);
sublistview= (Mylistview) Findviewbyid (R.id.sublistview);
}
private void Selectdefult () {
final int location=0;
Myadapter.setselectedposition (0);
Myadapter.notifydatasetinvalidated ();
Subadapter=new Subadapter (Getapplicationcontext (), cities, 0);
Sublistview.setadapter (Subadapter);
Sublistview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1,
int position, long Arg3) {
TODO auto-generated Method Stub
Toast.maketext (Getapplicationcontext (), cities[location][position], Toast.length_short). Show ();
}
});
}
}
The default I selected the No. 0 one, the following look at the effect of the operation: