我有一个RelativeLayout和一个ListView。我正在使用自定义布局和适配器。
加载应用时,焦点位于列表项的顶部。当我滚动(甚至只是一点点)时,列表会自动向下滚动到底部。之后,当我尝试向上滚动时,列表不会保持滚动位置。它落到了列表的底部。
布局:
<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"
tools:context="com.javapapers.android.listview.ArrayAdapterListViewActivity">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:divider="@null"
android:transcriptMode="disabled"
/>
</RelativeLayout>
的活动:
public class ScheduleActivity extends Activity {
private static final String TAG = "ScheduleActivity";
private MatchArrayAdapter matchArrayAdapter;
private ListView listView;
private static int colorIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule_layout);
colorIndex = 0;
listView = (ListView) findViewById(R.id.listview);
matchArrayAdapter = new MatchArrayAdapter(getApplicationContext(), R.layout.match_layout);
List<String[]> matchList = readData();
for(String[] matchData:matchList ) {
String teamA = matchData[0];
String teamAFlagRes = matchData[1];
String matchStart = matchData[2];
String teamB = matchData[3];
String teamBFlagRes = matchData[4];
int teamAFlagResId = getResources().getIdentifier(teamAFlagRes, "drawable", "com.javapapers.android.customlistview.app");
int teamBFlagResId = getResources().getIdentifier(teamBFlagRes, "drawable", "com.javapapers.android.customlistview.app");
Match match = new Match(teamA,
teamB,
matchStart,
teamAFlagResId,
teamBFlagResId,
getNextColor());
matchArrayAdapter.add(match);
}
}
适配器:
public class MatchArrayAdapter extends ArrayAdapter<Match> {
private static final String TAG = "MatchArrayAdapter";
private List<Match> matchList = new ArrayList<Match>();
static class MatchViewHolder {
ImageView teamAFlag;
ImageView teamBFlag;
TextView teamA;
TextView teamB;
TextView matchStart;
}
public MatchArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public void add(Match object) {
matchList.add(object);
super.add(object);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MatchViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.match_layout, parent, false);
viewHolder = new MatchViewHolder();
viewHolder.teamAFlag = (ImageView) row.findViewById(R.id.teamAFlag);
viewHolder.teamBFlag = (ImageView) row.findViewById(R.id.teamBFlag);
viewHolder.teamA = (TextView) row.findViewById(R.id.teamA);
viewHolder.teamB = (TextView) row.findViewById(R.id.teamB);
viewHolder.matchStart = (TextView) row.findViewById(R.id.matchStart);
row.setTag(viewHolder);
} else {
viewHolder = (MatchViewHolder)row.getTag();
}
Match match = getItem(position);
viewHolder.teamAFlag.setImageResource(match.getTeamAFlag());
viewHolder.teamA.setText(match.getTeamA());
viewHolder.matchStart.setText(match.getMatchStart());
viewHolder.teamBFlag.setImageResource(match.getTeamBFlag());
viewHolder.teamB.setText(match.getTeamB());
row.setBackgroundResource(match.getMatchColor());
return row;
}
答案 0 :(得分:0)
我在使用intel x86模拟器时遇到了这个问题。我已经改为ARM EABI模拟器,问题现在解决了。