将listView的每一行图标添加到抽屉中

时间:2014-09-19 15:49:18

标签: android xml bitmap drawerlayout

我希望非常明确地解释所有内容,正如标题所说,我如何在每行旁边插入图标,但是图片不是从drawable中提取的,而是通过URL

我知道使用drawable来完成此任务,但我不知道如何使用以下URL图像执行此操作:

URL url = new URL(stringURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
overlay = BitmapFactory.decodeStream(input);
connection.disconnect();

所以这是一个bitMap,而不是一个可绘制的例如。(。png,.jpg ......),

我的活动:

public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ListView mDrawerListR;
private ActionBarDrawerToggle mDrawerToggle;
private LinearLayout rightDrawerLinearLayout;
private String[] drawerItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerItems = getResources().getStringArray(R.array.array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerItems));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    rightDrawerLinearLayout = (LinearLayout) findViewById(R.id.right_drawer_ll);
    mDrawerListR = (ListView) findViewById(R.id.right_drawer);
    mDrawerListR.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerItems));
    mDrawerListR.setOnItemClickListener(new DrawerItemClickListener());
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
    seekBar.setOnTouchListener(this);
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close );
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}
[...]

activity_main.xml中

    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#123456"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <LinearLayout
        android:id="@+id/right_drawer_ll"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="#123456"
        android:orientation="vertical"
        android:visibility="visible" >

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="14dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="10dp"
            android:max="5"
            android:progress="0" />

        <ListView
            android:id="@+id/right_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#123456"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

drawer_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="#fff" />

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果要从URL加载图像,可以使用像...这样的库

https://github.com/nostra13/Android-Universal-Image-Loader

您可以通过适配器异步加载图像..