我最近在Android Studio中成功创建了两行列表视图。但是,我现在想要做的是对包含以该字母开头的项目的字母表的字母实现字母滚动和节标题,例如以A开头的项目位于名为' A'的标题标题下。我还希望包含空内容的字母的节标题不会出现,即字母“W' W'如果我的列表中没有任何项目请求W,那么我不希望W' W' W' W'部分标题出现等。
下面的图片是我的活动目前的样子,下面的图片是我想要实现的(每个项目两行而不是一行)。下面的4个代码段代表了我用来创建列表视图的内容(如第一张图片所示)。
请记住,所有需要做的就是添加一个字母滚动条和章节标题,因此,如果在给出答案时提供最有效的方法来实现上述内容并指出是否需要,我将非常感激。被添加,带走甚至合并。
actitivty_bakerloo_line.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.apptacularapps.exitsexpertlondonlite.BakerlooLineActivity">
<ListView
android:id="@+id/BakerloolineListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
BakerlooLineActivity.java
package com.apptacularapps.exitsexpertlondonlite;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class BakerlooLineActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bakerloo_line);
getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#B36305")));
getActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>Bakerloo line</font>"));
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv = (ListView) findViewById(R.id.BakerloolineListView);
lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv.getItemAtPosition(position);
SearchResults fullObject = (SearchResults)o;
}
});
}
private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr = new SearchResults();
sr.setStation("Baker Street");
sr.setFareZone("Fare zone 1");
results.add(sr);
sr = new SearchResults();
sr.setStation("Charing Cross");
sr.setFareZone("Fare zone 1");
results.add(sr);
sr = new SearchResults();
sr.setStation("Edgware Road");
sr.setFareZone("Fare zone 1");
results.add(sr);
return results;
}
}
SearchResults.java
package com.apptacularapps.exitsexpertlondonlite;
public class SearchResults {
private String station = "";
private String fareZone = "";
public void setStation(String station) {
this.station = station;
}
public String getStation() {
return station;
}
public void setFareZone(String fareZone) {
this.fareZone = fareZone;
}
public String getFareZone() {
return fareZone;
}
}
MyCustomBaseAdapter.java
package com.apptacularapps.exitsexpertlondonlite;
import java.util.ArrayList;
import com.apptacularapps.exitsexpertlondonlite.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.dualline_row_view, null);
holder = new ViewHolder();
holder.txtStation = (TextView) convertView.findViewById(R.id.station);
holder.txtFareZone = (TextView) convertView.findViewById(R.id.farezone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtStation.setText(searchArrayList.get(position).getStation());
holder.txtFareZone.setText(searchArrayList.get(position).getFareZone());
return convertView;
}
static class ViewHolder {
TextView txtStation;
TextView txtFareZone;
}
}
边列表
package com.apptacularapps.exitsexpertlondonlite;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class Bakerloo_line extends Activity {
ListView sideList;
ArrayList<Item> items = new ArrayList<Item>();
ListView listview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bakerloo_line);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#B36305")));
getActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>Bakerloo line</font>"));
listview=(ListView)findViewById(R.id.listView_Bakerlooline);
items.add(new SectionItem("My Friends"));
items.add(new EntryItem("Abhi Tripathi", "Champpu"));
items.add(new EntryItem("Sandeep Pal", "Sandy kaliya"));
items.add(new EntryItem("Amit Verma", "Budhiya"));
items.add(new EntryItem("Awadhesh Diwaker ", "Dadda"));
items.add(new SectionItem("Android Version"));
items.add(new EntryItem("Jelly Bean", "android 4.2"));
items.add(new EntryItem("IceCream Sandwich", "android 4.0"));
items.add(new EntryItem("Honey Comb", "android 3.0"));
items.add(new EntryItem("Ginger Bread ", "android 2.2"));
items.add(new SectionItem("Android Phones"));
items.add(new EntryItem("Samsung", "Galaxy"));
items.add(new EntryItem("Sony Ericson", "Xperia"));
items.add(new EntryItem("Nokia", "Lumia"));
EntryAdapter adapter = new EntryAdapter(this, items);
listview.setAdapter(adapter);
sideList = (ListView)findViewById(R.id.sideIndex);
List<String> sideIndexList = new ArrayList<String>();
sideIndexList.add("A");
sideIndexList.add("B");
sideIndexList.add("C");
sideIndexList.add("D");
sideIndexList.add("E");
sideIndexList.add("F");
sideIndexList.add("G");
sideIndexList.add("H");
sideIndexList.add("I");
sideIndexList.add("J");
sideIndexList.add("K");
sideIndexList.add("L");
sideIndexList.add("M");
sideIndexList.add("N");
sideIndexList.add("O");
sideIndexList.add("P");
sideIndexList.add("Q");
sideIndexList.add("R");
sideIndexList.add("S");
sideIndexList.add("T");
sideIndexList.add("U");
sideIndexList.add("V");
sideIndexList.add("W");
sideIndexList.add("X");
sideIndexList.add("Y");
sideIndexList.add("Z");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.side_index_row,sideIndexList );
sideList.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//When clicked, go to specific activity
if(position == 0) {
//code specific to first list item
Intent myIntent = new Intent(view.getContext(), Bakerloo_line.class);
startActivityForResult(myIntent, 0);
}
}
});
}
}
XML页面
<ListView
android:id="@+id/listView_Bakerlooline"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:fastScrollEnabled="true" />
<ListView
android:id="@+id/sideIndex"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:background="#ff996633"
android:divider="@null"
android:dividerHeight="0dp"
android:orientation="vertical" />
</LinearLayout>
side_index_row.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/side_index_row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/white"
android:textSize="14sp"
android:gravity="center">
</TextView>
答案 0 :(得分:2)
您应该开始更改列表标题列表视图的列表视图。谷歌的例子。