在写一个 SlidingDrawer的Demo时,发现一个自己原来没有注意的问题。
首先贴上源代码。
- package com.example.slidingdrawerproject;
- import android.R.anim;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.content.res.Resources;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.SlidingDrawer;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.ZoomControls;
- public class SlidingDrawerActivity extends Activity {
- private SlidingDrawer drawer = null;
- private ListView listView = null;
- private ArrayAdapter<CharSequence> adapter = null;
- private TextView textView = null;
- private ZoomControls controls = null;
- private float curFontSize = 0;
- private String[] data = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.activity_sliding_drawer);
- drawer = (SlidingDrawer) super.findViewById(R.id.slidingdrawer);
- LinearLayout layout = (LinearLayout) super.findViewById(R.id.content);
- listView = new ListView(this);
- adapter = ArrayAdapter.createFromResource(this, R.array.citys, android.R.layout.simple_list_item_1); // Resources resources = getResources(); // data = resources.getStringArray(R.array.citys); // adapter = new ArrayAdapter<CharSequence>(this, // android.R.layout.simple_list_item_1, data);
- listView.setAdapter(adapter);
- layout.addView(listView, 2);
- drawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
- @Override
- public void onDrawerClosed() {
- Toast.makeText(SlidingDrawerActivity.this, "关闭组件",
- Toast.LENGTH_SHORT).show();
- }
- });
- drawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
- @Override
- public void onDrawerOpened() {
- Toast.makeText(SlidingDrawerActivity.this, "打开组件",
- Toast.LENGTH_SHORT).show();
- }
- });
- drawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
- @Override
- public void onScrollStarted() {
- Toast.makeText(SlidingDrawerActivity.this, "滑动组件ing....",
- Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onScrollEnded() {
- Toast.makeText(SlidingDrawerActivity.this, "滑动组件end",
- Toast.LENGTH_SHORT).show();
- }
- });
- listView.setOnItemClickListener(new ListView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- Object obj = parent.getAdapter().getItem(position);
- if (obj instanceof String) {
- String info = (String) obj;
- Dialog dialog = new AlertDialog.Builder(
- SlidingDrawerActivity.this).setTitle("info")
- .setMessage(info).setIcon(R.drawable.ic_launcher)
- .create();
- dialog.show();
- }
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_sliding_drawer, menu);
- return true;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="citys">
- <item>北京</item>
- <item>南昌</item>
- <item>昆明</item>
- <item>九江</item>
- <!-- (云南过桥米线发源地) -->
- <item>蒙自</item>
- <item>深证</item>
- <item>东莞</item>
- <item>香港</item>
- </string-array>
- </resources>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <SlidingDrawer
- android:id="@+id/slidingdrawer"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:handle="@+id/handle"
- android:content="@+id/content"
- >
- <ImageView
- android:id="@+id/handle"
- android:src="@android:drawable/ic_lock_idle_alarm"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <LinearLayout
- android:id="@+id/content"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/slid_txt"
- android:text="明天放假"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <ZoomControls
- android:id="@+id/slid_zoomCon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- </SlidingDrawer>
- </LinearLayout>
效果图:
这是可以出现效果的。
在ArrayAdapter中的源代码中:是
- adapter = ArrayAdapter.createFromResource(this, R.array.citys,
- android.R.layout.simple_list_item_1);
这个是没有问题的,查API文档,
public static ArrayAdapter<CharSequence> createFromResource (Context context, int textArrayResId, int textViewResId)
Since:
API Level 1
Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int)
.
Parameters
context | The application's environment. |
---|---|
textArrayResId | The identifier of the array to use as the data source.//xml中array的id |
textViewResId | The identifier of the layout used to create views.//xml中layout的id |
Returns
- An ArrayAdapter.
可是由于自己的粗心,把代码写成
- adapter = new ArrayAdapter<CharSequence>(this,
- android.R.layout.simple_list_item_1,R.array.citys);
而且由于类型匹配,所以系统没有报错。
后来查过API
public ArrayAdapter (Context context, int resource, int textViewResourceId)
Since:
API Level 1
Constructor
Parameters
context | The current context. |
---|---|
resource | The resource ID for a layout file containing a layout to use when instantiating views. |
textViewResourceId | The id of the TextView within the layout resource to be populated//TextView在layout中的id |
所以自己错写的方法是不匹配的。
这里还有两种方法写这个adapter:首先看API文档
public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
Since:
API Level 1
Constructor
Parameters
context | The current context. |
---|---|
textViewResourceId | The resource ID for a layout file containing a TextView to use when instantiating views. |
objects | The objects to represent in the ListView. |
①
将数据在代码中写成数组形式,
- private String[] pro=new String[]{"北京","上海","广东","江西","云南","福建","海南","河南"};
- adapter = new ArrayAdapter<CharSequence>(this,
- android.R.layout.simple_list_item_1, data);
②将xml中的资源通过resources的getStringArray()方法转化为数组。
- Resources resources = getResources();
- data = resources.getStringArray(R.array.citys);
- adapter = new ArrayAdapter<CharSequence>(this,
- android.R.layout.simple_list_item_1, data);
转载于:https://blog.51cto.com/3861969/1009582