抽屉中的适配器和动态数据

时间:2016-12-25 07:40:26

标签: android

我是Android编程的新手,面临一些问题。

我想实现这个

enter image description here

这是我对网站的看法。我试图在android上实现相同的功能。 enter image description here

已经完成了很多工作,但是我无法继续前进,而且我遇到了Button事件。这些是抽屉,数据是使用适配器设置的。

我的activity_main.xml文件

    <android.support.v4.widget.DrawerLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context="com.gyanibano.drawerlayout.MainActivity">
    <include layout="@layout/app_bar"/>
        <ListView
            android:layout_marginTop="?attr/actionBarSize"
            android:id="@+id/drawer_left"
            android:divider="@android:color/transparent"
            android:layout_gravity="start"
            android:layout_width="250dp"
            android:layout_height="match_parent"
            android:padding="0dp"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:background="#ffffff">

        </ListView>
        <ListView
            android:layout_marginTop="?attr/actionBarSize"
            android:id="@+id/drawer_right"
            android:divider="@android:color/transparent"
            android:layout_gravity="end"
            android:layout_width="250dp"
            android:layout_height="match_parent"
            android:padding="20dp"
            android:horizontalSpacing="10dp"
            android:verticalSpacing="10dp"
            android:background="#ffffff">
        </ListView>
</android.support.v4.widget.DrawerLayout>

我的gdmainleft.xml文件

    <LinearLayout xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="20dp"
    android:background="@drawable/border_set"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/sideColor"
            android:layout_width="8dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#88FF0000"
            android:layout_marginRight="5dp"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentLeft="true" />

            <TextView
                android:id="@+id/txt"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_alignBottom="@+id/imageView1"
                android:layout_alignParentRight="true"
                android:textColor="@android:color/black"
                android:layout_alignTop="@+id/imageView1"
                android:layout_toRightOf="@+id/imageView1"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </RelativeLayout>

        <TextView
            android:id="@+id/count"
            android:layout_gravity="center"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:textSize="26sp" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我的CustomerAdapter.java文件

public class CustomerAdapter extends BaseAdapter {
    String result[];
    Context context;
    String counts[];
    int img[], colr[];
    int no;
    int showquest;
    private static LayoutInflater inflater=null;

    public CustomerAdapter(String[] result, Context context, String[] counts, int[] img, int colr[], int no, int showquest) {
        this.result = result;
        this.context = context;
        this.counts = counts;
        this.img = img;
        this.colr = colr;
        this.no = no;
        this.showquest = showquest;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount(){
        return result.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final holder hld = new holder();
        View rowview, questview;
        if(no==1) {
            rowview = inflater.inflate(R.layout.gdmainright, null);
            hld.tv = (TextView) rowview.findViewById(R.id.txt);
            hld.tv.setText(result[i]);
        }
        else{

            rowview = inflater.inflate(R.layout.gdmainleft, null);
            hld.tv = (TextView) rowview.findViewById(R.id.txt);
            hld.tv.setText(result[i]);

            hld.tv = (TextView) rowview.findViewById(R.id.count);
            hld.tv.setText(counts[i]);

            hld.tv = (TextView) rowview.findViewById(R.id.sideColor);
            hld.tv.setBackgroundColor(colr[i]);

            hld.iv = (ImageView) rowview.findViewById(R.id.imageView1);
            hld.iv.setImageResource(img[i]);
        }
        return rowview;
    }

    public class holder{
        TextView tv;
        ImageView iv;
        Button bt;
    }

}

我的MainActivity.java文件

                usercount = jObj.getInt("usercount");
                mcqcount = jObj.getInt("mcqcount");
                keywordscount = jObj.getInt("keywordscount");
                papercount = jObj.getInt("papercount");
                showquest = jObj.getInt("showquest");

                Tags = " QnA, Tags, Papers, Users";
                tagname = Tags.split(",");

                Counts = mcqcount+","+keywordscount+","+papercount+","+usercount;
                countvalue = Counts.split(",");

                Imgs = new int[]{R.drawable.qm, R.drawable.tg, R.drawable.bk, R.drawable.usr};

                Colr = new int[]{Color.rgb(255,0,0), Color.rgb(255, 165, 0),Color.rgb(102,204,87), Color.rgb(0,0,255)};

                cl3 = new CustomerAdapter(tagname, con, countvalue, Imgs, Colr, 2, showquest);
                drawer_left.setAdapter(cl3);

0 个答案:

没有答案