水平滚动视图与子项目

时间:2015-07-13 07:54:24

标签: android

我需要水平滚动视图和一些项目。如果我点击其中一个项目,它应该显示特定主要项目的子项目。它应该发生在同一个屏幕上。 请帮助!!

Android_horizo​​ntal.java

public class Android_HorizontalListViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listviewdemo);

    HorizontalListView listview = (HorizontalListView) findViewById(R.id.listview);
    listview.setAdapter(new HAdapter());

}

private static String[] dataObjects = new String[]{ "Text #1",
    "Text #2",
    "Text #3",
    "Text #4",
    "Text #5",
    "Text #6"}; 

private class  HAdapter extends BaseAdapter {

    public HAdapter(){
        super();
    }
    private OnClickListener mOnButtonClicked = new OnClickListener() {

        public void onClick(View v) {
            Button bt = (Button) v;

            Toast.makeText(getApplicationContext(), "Hi " + bt.getText(), Toast.LENGTH_SHORT).show();           
        }
    };


    public int getCount() {
        return dataObjects.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View retval = LayoutInflater.from(parent.getContext()).inflate(R.layout.viewitem, null);
        TextView title = (TextView) retval.findViewById(R.id.title);
        Button button = (Button) retval.findViewById(R.id.clickbutton);
        button.setOnClickListener(mOnButtonClicked);
        title.setText(dataObjects[position]);

        return retval;
    }

};

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Relative"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/waitergateback"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/LL1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
<ImageView
        android:id="@+id/waitericon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:src="@drawable/waitericon" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/LL1"
    android:background="@drawable/waitergateback"
    android:orientation="vertical" >

<ImageView
        android:id="@+id/image"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher"
        android:layout_marginLeft="15dp"/>

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:gravity="center_horizontal"
        android:padding="20dp"
        android:text="Item One"
        android:textColor="#000" />

<Button
        android:id="@+id/clickbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:padding="20dp"
        android:text="Click Me"
        android:textColor="#000" />
</LinearLayout>

horizo​​ntalview.xml

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical" >

<com.devsmart.android.ui.HorizontalListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ddd" />
</LinearLayout>

0 个答案:

没有答案