左侧抽屉的实现问题

Android 码拜 10年前 (2015-05-11) 1054次浏览 0个评论
 

想要实现的效果(抽屉上的listview上面每个项都有一个图标)

左侧抽屉的实现问题

下面是我的布局文件(目前还不知道怎么加载另外的布局文件,所以左侧抽屉和中间部分的布局都在一个文件)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 >
    <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        <!-- The main content view -->
        <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            <ListView
                    android:id="@+id/show"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
            <ImageView
                    android:id="@+id/bimg"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
            <TextView
                    android:id="@+id/shuosha"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    />
        </FrameLayout>
        <!-- The navigation drawer         -->
        <ListView
                android:id="@+id/left_drawer"
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="#111"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="0dp" />
    </android.support.v4.widget.DrawerLayout>


</RelativeLayout>

下面是Activity文件(参照了一些网上的例子):

import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.*;
import android.widget.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

import android.support.v4.widget.DrawerLayout;

public class MainActivity extends ListActivity {

    /*初始化菜单*/
    private static final String TOULE ="偷乐";
    private static final String YANFU="眼福";
    private static final String SHUOSHA ="说啥";
    /**/

    private ListView show;//show qiubai.
    private ImageView bimg;//show bing image.
    private ListView vlist ;//list view

    //private ActionBarDrawerToggle mDrawerToggle;
    private Bitmap bingisources;//bing image sources.
    private List<String> qsbkshow = new ArrayList<String>();

    //private List <String> arrayToList = new ArrayList<String>(Arrays.asList(new String[]{"偷乐","眼福"}));

    private DrawerLayout mDrawerLayout = null;
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                if (bimg.getVisibility() == View.VISIBLE) {
                    bimg.setVisibility(View.INVISIBLE);
                }
                show.setVisibility(View.VISIBLE);
                show.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.listcustom, qsbkshow));
            } else if (msg.what == 0x124) {
                if (show.getVisibility() == View.VISIBLE) {
                    show.setVisibility(View.INVISIBLE);
                }
                bimg.setVisibility(View.VISIBLE);
                bimg.setImageBitmap(bingisources);
            }
            mDrawerLayout.setVisibility(View.INVISIBLE);
        }
    };


    /**
     * 获取左侧菜单的listView.
     *
     *
     * @return
     */
    private List<Map<String, Object>> getData(){
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(TOULE,R.drawable.toule);
        map.put(YANFU,R.drawable.yanfu);
        map.put(SHUOSHA,R.drawable.guanyu);
        list.add(map);
        return list;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.leftmenu:
                if(mDrawerLayout.getVisibility()==View.VISIBLE){
                    //vlist.setVisibility(View.INVISIBLE);
                }else{
                    mDrawerLayout.setVisibility(View.VISIBLE);
                    mDrawerLayout.openDrawer(Gravity.LEFT);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(vlist);
        menu.findItem(R.id.leftmenu).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setContentView(R.layout.main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        show = (ListView) findViewById(R.id.show);
        bimg = (ImageView) findViewById(R.id.bimg);
        vlist= (ListView) findViewById(R.id.left_drawer);
        setListAdapter(new SimpleAdapter(MainActivity.this, getData(), android.R.layout.simple_list_item_2, new String[]{"img_pre", "text"}, new
                int[]{R.id.img_pre, R.id.text}));
        vlist = getListView();
        vlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if ((getData().get(0)).get(position).equals(TOULE)) {
                    class getNet implements Runnable {
                        @Override
                        public void run() {
                            getqs();
                        }
                    }
                    new Thread(new getNet()).start();
                }else if ((getData().get(0)).get(position).equals(YANFU)) {
                    class getBingimages implements Runnable {
                        @Override
                        public void run() {
                            try {
                                getbimsa();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    new Thread(new getBingimages()).start();
                }else if ((getData().get(0)).get(position).equals(SHUOSHA)) {
                    TextView tv = (TextView) findViewById(R.id.shuosha);
                    Toast.makeText(MainActivity.this,"说啥,现在看到了把??哈哈",Toast.LENGTH_LONG);
                    tv.setText("暂时不说.");
                }
            }
        });
    }
}


在线等大神……

40分
推荐使用github上的抽屉框架
https://github.com/ikimuhendis/LDrawer

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明左侧抽屉的实现问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!