从顶部排列 GridView 项目
Arrange the GridView items from the top
我下载了一个源并进入那个源
使用了一个 GridView
我希望项目自上而下
但是顺序是从下往上
我该怎么办
从上到下排列
喜欢下面的照片
这些是我的代码
<GridView
android:layout_width="match_parent"
android:layout_height="130dp"
android:stretchMode="columnWidth"
android:numColumns="6"
android:layoutDirection="rtl"
android:id="@+id/grid_view_item_details_1"/>
和
public class SquaresAdapters extends BaseAdapter {
private Activity mActivity;
private List<Square> mList;
public SquaresAdapters(Activity activity, List<Square> squares) {
mActivity = activity;
mList = squares;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View choice;
LayoutInflater inflater = mActivity.getLayoutInflater();
if (convertView == null) {
choice = inflater.inflate(R.layout.square_view, parent, false);
TextView textChoice = (TextView) choice;
if (!mList.get(position).getState()) {
textChoice.setBackground(AppCompatResources.getDrawable(mActivity, R.drawable.background_squares_with_empty_state));
} else {
textChoice.setBackground(AppCompatResources.getDrawable(mActivity, R.drawable.background_squares_with_populated_state));
}
textChoice.setText(mList.get(position).getLetter());
if (mList.get(position).getTextColor().equals("black")) {
textChoice.setTextColor(ContextCompat.getColor(mActivity, R.color.black_dark));
} else {
textChoice.setTextColor(ContextCompat.getColor(mActivity, R.color.green16));
}
} else {
choice = convertView;
}
return choice;
}
public void setSquaresList(List<Square> list) {
mList = list;
}
}
反转传递给适配器的数据,如
public SquaresAdapters(Activity activity, List<Square> squares) {
mActivity = activity;
mList = squares;
Collections.reverse(mList);// this will reverse the list
}
如果您在创建适配器后设置数据,也可以在此处反转
public void setSquaresList(List<Square> list) {
mList = list;
Collections.reverse(mList);
notifyDataSetChanged();
}
将 getItem 更改为此
@Override
public Object getItem(int position) {
return mList.get(position);
}
老实说,这是一个愚蠢的解决方案,但它会给你一个想法:
public SquaresAdapters(Activity activity, List<Square> squares) {
mActivity = activity;
mList = new ArrayList<>();
for(int i = 3; i < 6; i++) {
mList.add(squares.get(i));
}
for(int i = 0; i < 3; i++) {
mList.add(squares.get(i));
}
}
如果你使用现成的源代码并且你有这个问题
可能在代码的某个地方,这将使项目以相反的方式出现
搜索并找到该代码并编辑或删除它
类似代码
Collections.reverse(mList);
和
for (int i = list.size(); i > 0; i--)
等等
我下载了一个源并进入那个源
使用了一个 GridView
我希望项目自上而下
但是顺序是从下往上
我该怎么办
从上到下排列
喜欢下面的照片
这些是我的代码
<GridView
android:layout_width="match_parent"
android:layout_height="130dp"
android:stretchMode="columnWidth"
android:numColumns="6"
android:layoutDirection="rtl"
android:id="@+id/grid_view_item_details_1"/>
和
public class SquaresAdapters extends BaseAdapter {
private Activity mActivity;
private List<Square> mList;
public SquaresAdapters(Activity activity, List<Square> squares) {
mActivity = activity;
mList = squares;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View choice;
LayoutInflater inflater = mActivity.getLayoutInflater();
if (convertView == null) {
choice = inflater.inflate(R.layout.square_view, parent, false);
TextView textChoice = (TextView) choice;
if (!mList.get(position).getState()) {
textChoice.setBackground(AppCompatResources.getDrawable(mActivity, R.drawable.background_squares_with_empty_state));
} else {
textChoice.setBackground(AppCompatResources.getDrawable(mActivity, R.drawable.background_squares_with_populated_state));
}
textChoice.setText(mList.get(position).getLetter());
if (mList.get(position).getTextColor().equals("black")) {
textChoice.setTextColor(ContextCompat.getColor(mActivity, R.color.black_dark));
} else {
textChoice.setTextColor(ContextCompat.getColor(mActivity, R.color.green16));
}
} else {
choice = convertView;
}
return choice;
}
public void setSquaresList(List<Square> list) {
mList = list;
}
}
反转传递给适配器的数据,如
public SquaresAdapters(Activity activity, List<Square> squares) {
mActivity = activity;
mList = squares;
Collections.reverse(mList);// this will reverse the list
}
如果您在创建适配器后设置数据,也可以在此处反转
public void setSquaresList(List<Square> list) {
mList = list;
Collections.reverse(mList);
notifyDataSetChanged();
}
将 getItem 更改为此
@Override
public Object getItem(int position) {
return mList.get(position);
}
老实说,这是一个愚蠢的解决方案,但它会给你一个想法:
public SquaresAdapters(Activity activity, List<Square> squares) {
mActivity = activity;
mList = new ArrayList<>();
for(int i = 3; i < 6; i++) {
mList.add(squares.get(i));
}
for(int i = 0; i < 3; i++) {
mList.add(squares.get(i));
}
}
如果你使用现成的源代码并且你有这个问题 可能在代码的某个地方,这将使项目以相反的方式出现
搜索并找到该代码并编辑或删除它
类似代码
Collections.reverse(mList);
和
for (int i = list.size(); i > 0; i--)
等等