Android ListView中的样式项视图

时间:2015-06-14 21:13:12

标签: android listview

我无法弄清楚我的ListView出了什么问题,因为它太小了。我想要更大的项目,如Android上的默认列表视图。

SelectContactActivity

public class SelectContactActivity extends Activity {

private ArrayList<Contact> listContacts = new ArrayList<Contact>();

private ArrayList<SongInfo> listSong = new ArrayList<SongInfo>();
private ListContactsAdapter adapter;
private Util util = new Util();
private ListView list;
private EditText txt_search;
private ArrayList<Contact> listSearch;

private Handler guiThread;

private Runnable updateTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.mycontacts);

list = (ListView)findViewById(R.id.list);
txt_search = (EditText)findViewById(R.id.txt_search);

final int position = this.getIntent().getIntExtra("position", 0);

listSong = util.getAllSong(this);

listContacts = util.getAllContact(this);

Log.i("LOG", "Size: " + listContacts.size());

adapter = new ListContactsAdapter(this,   android.R.layout.simple_list_item_1, listContacts);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);

list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
util.assignRingtoneToContact(SelectContactActivity.this,
listSong.get(position), listContacts.get(arg2));
Toast.makeText(
SelectContactActivity.this,
"Ringtone set successfully",
Toast.LENGTH_LONG).show();
finish();
}
});

innitThread();

txt_search.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
queueUpdate(500);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void afterTextChanged(Editable s) {

}
});

}

private void queueUpdate(long delayMillisecond) {
guiThread.removeCallbacks(updateTask);
// update data if no change in textSearch after time config
// timer by = milliseconds
guiThread.postDelayed(updateTask, delayMillisecond);
}

private void innitThread() {
guiThread = new Handler();
updateTask = new Runnable() {

@Override
public void run() {

String word = txt_search.getText().toString().trim();
if (word.equalsIgnoreCase("")) {
// if not change set listView first
list.setAdapter(new  ListContactsAdapter(SelectContactActivity.this,
android.R.layout.simple_list_item_1, listContacts));
} else
// if txtSearch not null
{

// get data from webservice
getDataByKeywords(word);
// Show on list
listSearch = new ArrayList<Contact>();

// get data from webservice
listSearch = getDataByKeywords(word);

list.setAdapter(new ListContactsAdapter(SelectContactActivity.this,  android.R.layout.simple_list_item_1, listSearch));
adapter.notifyDataSetChanged();
}

}
};
}

public ArrayList<Contact> getDataByKeywords(String keyword) {
listSearch = new ArrayList<Contact>();
keyword = keyword.toUpperCase();
for (int i = 0; i < listContacts.size(); i++) {
String contain = listContacts.get(i).getName().toUpperCase();
if (contain.contains(keyword)) {
listSearch.add(listContacts.get(i));
}
}
return listSearch;
}

}

ListContactsAdapter

public class ListContactsAdapter extends ArrayAdapter<Contact>{

private ArrayList<Contact> contacts;
private Context context;

public ListContactsAdapter(Context context, int textViewResourceId,
ArrayList<Contact> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.contacts = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView!=null){
convertView.setBackgroundResource(R.drawable.list_selector);
}

TextView textView = getGenericView();
textView.setBackgroundResource(R.drawable.list_selector);
textView.setText(contacts.get(position).getName());
return textView;
}

public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 70);

TextView textView = new TextView(context);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(16, 0, 0, 0);
textView.setTextSize(18);
textView.setShadowLayer(1, 1, 1, Color.BLACK);
textView.setTextColor(0xffeeeeee);
return textView;
}

}

mycontacts.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
android:id="@id/relativeLayoutSearch"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:gravity="center_vertical"
android:paddingLeft="12dp"
android:paddingRight="12dp" >

<EditText
android:id="@id/txt_search"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/search_bar"
android:hint="@string/hint_apps_search"
android:paddingBottom="12dp"
android:paddingLeft="45.0dip"
android:paddingRight="14dp"
android:paddingTop="12dp"
android:singleLine="true"
android:textSize="15.0sp" />

<Button
android:id="@id/button2"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/zoomicon" />

</RelativeLayout>

<ListView
android:id="@id/list"
style="@style/ContactList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/relativeLayoutSearch"
android:cacheColorHint="#e0000000" />
</RelativeLayout>

styles.xml

    <style name="ContactList">
    <!-- <item name="android:background">@color/listbg</item> -->
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>
    <item name="android:cacheColorHint">#e0000000</item>
    <item name="android:divider">@color/listdiv</item>
    <item name="android:dividerHeight">1.0dip</item>
    </style>

这是我的联系人列表代码,下面是截图,但我希望列表中有更大的项目。有什么建议?

当前列表视图:

Current listview

1 个答案:

答案 0 :(得分:0)

我会通过重新访问你的适配器来缩小规模。 ListView本身非常简单 - 在您的活动布局中,您将ListView设置为match_parent的宽度和高度。

适配器是创建每一行的组件,在ListAdapter中由getView()方法启动。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView != null) {
        convertView.setBackgroundResource(R.drawable.list_selector);
    }

    TextView textView = getGenericView();
    textView.setBackgroundResource(R.drawable.list_selector);
    textView.setText(contacts.get(position).getName());
    return textView;
}

注意你在这里做的不正确;你对convertView做了些什么,但是你忽略它,然后创建一个新的视图。模式更像是:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (rowView == null) {
        rowView = // create a new View that represents your row
    }
    // bind the data to rowView, then return it
    return rowView;
}

在你的情况下可能是:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView rowView = (TextView) convertView;
    if (rowView == null) {
        rowView = getGenericView();
        rowView.setBackgroundResource(R.drawable.list_selector);
    }
    rowView.setText(contacts.get(position).getName());
    return rowView;
}

请参阅,如果它为空,则只需创建rowView。此外,背景只需要设置一次(如果需要,可以用XML完成)。

通过创建行View,我建议首先将包含单个TextView的布局作为唯一元素进行操作。

view_item_contact.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

然后您的getGenericView()可以重命名为createContactRowView()

private TextView createContactRowView(ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    return ((TextView) layoutInflater.inflate(R.layout.view_item_contact, parent, false));
}

从那里开始,您可以通过添加填充,设置最小高度,通过应用重力垂直居中文本等来开始在view_item_contact.xml中设置样式。

view_item_contact.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:minHeight="48dp"
  android:gravity="center_vertical"
  android:paddingLeft="16dp"
  android:paddingRight="16dp"
  android:background="@drawable/list_selector" />

在几乎所有情况下,我都会避免以编程方式创建视图 - 始终从XML中扩展它们,因此您可以将样式和布局与逻辑分开。