android list inside layout

后端 未结 2 877
执念已碎
执念已碎 2021-01-29 07:35

i am working on an Android app that needs showing a list[table], inside the layout[view]

I come from iPhone dev objC land, and i have an app that shows a table[list] ins

相关标签:
2条回答
  • I have try these example it's very nice.

    you can get the example from

    http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text?msg=4567162#xx4567162xx

    0 讨论(0)
  • 2021-01-29 08:05

    Yes, of course, you can do that

    1) you need to have listholder.xml here, you can scratch anything in you layout view, either imageview, textview..etc. just don't forget to add ListView inside it. for example:

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/head_logo_bg">
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_label">
        <TextView
            android:id="@+id/city_txt" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_gravity="center"
            android:text="Sydney"
            android:textStyle="bold"
            android:textSize="17sp"/>
    </LinearLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="40sp">
    
        <ListView 
        android:id="@android:id/list"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_centerVertical="true"
        android:scrollingCache="false"/>
    </LinearLayout>
    

    2) For custom your own list item, you have to create listitem.xml i.e.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listitemone"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10sp">
    
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical">
                <ImageView android:id="@+id/user_image"
                    android:layout_width="80px" android:layout_height="80px"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="5px"
                    android:src="@drawable/icon"
                    />
        </LinearLayout>
        <RelativeLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5sp"
            android:orientation="vertical">
                <TextView
                    android:id="@+id/date_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/date"
                    android:textStyle="bold"
                android:textSize="16sp" />
                <TextView
                    android:id="@+id/date_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignBaseline="@id/date_label"
                    android:layout_marginRight="20sp"
                    android:textColor="#FFF"
                    android:text="MM/dd/YYYY"
                    android:textStyle="bold"
                android:textSize="16sp" />
          </RelativeLayout>
    </LinerLayout>
    

    3) create customAdapter in your activity, it would look like this;

    public class MyListActivity extends ListActivity {
    
    private ArrayList<Yourdata> yourdata = new ArrayList<Youdata>();
            @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listholder);    
                   // yourdata might be array, arraylist etc. 
    
                   MyCustomAdapter listadapter = new MyCustomAdapter(this, R.layout.listitem, yourdata);
    
              setListAdapter(listadapter);
    }
    
    private class MyCustomAdapter extends ArrayAdapter<Yourdata>{
                    //this case, i use Yourdata as type
            private ArrayList<Yourdata> items;
    
            public PreviousAdapter(Context context, int textViewResourceId,
                    ArrayList<Yourdata> items) {
                super(context, textViewResourceId, items);
                this.items = items;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if(v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listitem, null);
                }
                Yourdata yt = items.get(position);
                if(yt != null){
                 // Don't forget to use v.findView...., otherwise, it might force close when run app.
                    TextView dateStr = (TextView)v.findViewById(R.id.date_value);
    
                        dateStr.setText(yt.getDate());
                }   
                return v;
            }
    
    
    
          }
    
    
    }
    

    P.S. the above code might not exactly right... just give you an idea :) Here is a source about custom list (you might have seen it) hope it useful

    http://www.vogella.de/articles/AndroidListView/article.html

    0 讨论(0)
提交回复
热议问题