gpt4 book ai didi

java - GridView android suffle 项目滚动时

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:28 24 4
gpt4 key购买 nike

我想在 GridView 中显示可垂直滚动的圆圈列表。我想要 3 列中的两个元素之一。

您可以看到结果的屏幕截图。

GridView after loading

但是我还有两个问题: - 当我在应用程序中向下滚动时,应用程序会重新绘制圆圈并将其打乱。(第二个屏幕)。 After scroll - 我不能有一个完美的圆形按钮,我不明白为什么。

为此,我有:我的 Activity 与 gridLayout

    gridView = (GridView) findViewById(R.id.interestsgrid);
gridView.setColumnWidth(UTIL.getScreenWidth(getBaseContext()) / 3);
List<Interest> interests = new ArrayList<Interest>();
interests.add(new Interest(18,"coucou7"));
interests.add(new Interest(18,"coucou7"));
interests.add(new Interest(18,"coucou6"));
interests.add(new Interest(18,"coucou6"));
interests.add(new Interest(18,"coucou5"));
interests.add(new Interest(18,"coucou5"));
interests.add(new Interest(18,"coucou4"));
interests.add(new Interest(18,"coucou4"));
interests.add(new Interest(18,"coucou3"));
interests.add(new Interest(18,"coucou3"));
interests.add(new Interest(18,"coucou8"));
interests.add(new Interest(18,"coucou8"));
interests.add(new Interest(18,"coucou9"));
interests.add(new Interest(18,"coucou9"));
interests.add(new Interest(18,"coucou10"));
interests.add(new Interest(18,"coucou10"));
interests.add(new Interest(18,"coucou11"));
interests.add(new Interest(18,"coucou12"));
interests.add(new Interest(18,"coucou13"));
interests.add(new Interest(18,"coucou13"));
interests.add(new Interest(18,"coucou14"));
interests.add(new Interest(18,"coucou14"));
gridView.setAdapter(new InterestsGridAdapter(this, interests));

Activity .xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
tools:context=".viewController.user.ChoiceInterests">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/AppBarLayoutInterest"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/interestsgrid"
android:layout_width="match_parent"
android:paddingTop="10sp"
android:layout_below="@+id/AppBarLayoutInterest"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>

和我的适配器:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// LayoutInflator to call external grid_item.xml file

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;


if (convertView == null) {

gridView = new View(context);

// get layout from grid_item.xml ( Defined Below )

gridView = inflater.inflate(R.layout.interest_item, null);

// set value into textview

Button buttonView = (Button) gridView
.findViewById(R.id.buttonCircleInterest);

int width = UTIL.getScreenWidth(parent.getContext());


ViewGroup.LayoutParams params = buttonView.getLayoutParams();
params.width = (width / 3);
params.height = (width / 3);
buttonView.setLayoutParams(params);

if(position%2 != 0){
buttonView.setVisibility(View.INVISIBLE);
}

buttonView.setText(this.interests.get(position).getName());

} else {
gridView = (View) convertView;
}

return gridView;
}
}

我的网格项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="50sp"
android:layout_height="50sp"
android:id="@+id/buttonCircleInterest"
android:background="@drawable/rounshapebtn"
android:textColor="@color/white"
android:layout_marginBottom="2sp"
android:text="1"/>
</LinearLayout>

我的 Drawable 有一个圆形按钮:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFA633"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="50dp"
android:bottomLeftRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp"/>
</shape>

感谢您的回答!

最佳答案

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// LayoutInflator to call external grid_item.xml file

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;


if (convertView == null) {

gridView = new View(context);

// get layout from grid_item.xml ( Defined Below )

gridView = inflater.inflate(R.layout.interest_item, null);


} else {
gridView = (View) convertView;
}

// set value into textview

Button buttonView = (Button) gridView
.findViewById(R.id.buttonCircleInterest);

int width = UTIL.getScreenWidth(parent.getContext());


ViewGroup.LayoutParams params = buttonView.getLayoutParams();
params.width = (width / 3);
params.height = (width / 3);
buttonView.setLayoutParams(params);

if(position%2 != 0){
buttonView.setVisibility(View.INVISIBLE);
}

buttonView.setText(this.interests.get(position).getName());


return gridView;
}
}

关于java - GridView android suffle 项目滚动时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34616227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com