gpt4 book ai didi

java - android 在 GridView 更新之间等待

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:20 25 4
gpt4 key购买 nike

我有一个 TextView 的 GridView,我想遍历每个 TextView,更改它的背景,等待 1 秒,然后转到下一个 TextView 并做同样的事情。

首先从没有着色的 TextView 开始

SoE1

然后我想给第一个TextView上色

SoE2

现在我想等一秒钟......

及之后。

SoE3

并重复其余的 TextView。

问题是,更新网格的方法 MainActivity.playSoE() 使用 notifyDataSetChanged() 来更新 GridView。这使得线程化成为一个问题。我尝试过的一切要么导致异常,要么卡住一切并跳过中间步骤,直到整个网格都被着色。我试过使用 Activity.runOnUiThread()wait()Thread.sleep() 并尝试创建一个新的 Thread 但这些都不适合我。我

澄清一下,我希望能够在为网格着色时与应用程序的其余部分进行通信,我不希望整个应用程序在我身上卡住。

主要 Activity .java:

public class MainActivity extends ActionBarActivity {

private boolean mIsPlaying;
private int primesLE;
private GridView mGridView;
private ImageAdapter mAdapter;
private Thread mThread;

private void dataVisualization(){

int numOfColumns = (int)Math.round(Math.sqrt((double) primesLE));
//int numOfRows = (int)Math.ceil((double)primesLE/(double)numOfColumns);
ViewGroup.LayoutParams layoutParams;

layoutParams = mGridView.getLayoutParams();
layoutParams.width = 150*numOfColumns; //this is in pixels
mGridView.setLayoutParams(layoutParams);
mGridView.setNumColumns(numOfColumns);
mAdapter = new ImageAdapter(this, android.R.layout.simple_list_item_1, primesLE);
mGridView.setAdapter(mAdapter);

}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mIsPlaying = false;
primesLE = 0;


View relativeLayout = findViewById(R.id.relativeLayout);
View title_horizontalScrollView = relativeLayout.findViewById(R.id.title_horizontalScrollView);
View dataLayout = title_horizontalScrollView.findViewById(R.id.dataLayout);
mGridView = (GridView) dataLayout.findViewById(R.id.mGridView);

dataVisualization();

View inputLayout = relativeLayout.findViewById(R.id.inputLayout);
final EditText inputEditText = (EditText)inputLayout.findViewById(R.id.inputEditText);
inputEditText.setOnKeyListener(new EditText.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press

InputMethodManager inputManager = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(inputEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

try {
primesLE = Integer.parseInt(inputEditText.getText().toString());
} catch(NumberFormatException nfe) {
inputEditText.setText("Try again");
primesLE = 0;
}
if(primesLE < 0)
primesLE = 0;

dataVisualization();

Toast.makeText(MainActivity.this, inputEditText.getText(), Toast.LENGTH_SHORT).show();

return true;
}
return false;
}
});
inputEditText.setOnClickListener(new EditText.OnClickListener() {
@Override
public void onClick(View v) {
inputEditText.getText().clear();
}
});
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

private void playSoE(){

for(int i = 0; i < primesLE; i++) {
mAdapter.setPositionColor(i, 0xffff0000 + 0x100 * i); //calls notifyDataSetChanged()
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
}, 1000);

}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Log.d("Menu","Button Pressed");
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

else if (id == R.id.action_status){
if(mIsPlaying) {
mIsPlaying = false;
item.setIcon(R.drawable.ic_action_play);
item.setTitle("Play");
playSoE();
}
else {
mIsPlaying = true;
item.setIcon(R.drawable.ic_action_pause);
item.setTitle("Pause");
}
return true;
}

return super.onOptionsItemSelected(item);
}
}

ImageAdapter.java:

public class ImageAdapter extends ArrayAdapter {
private Context mContext;
private int mCount;
private boolean setBlueBackground = false;
private int[] gridColors;

public ImageAdapter(Context c, int resource, int count) {
super(c, resource);
mContext = c;
mCount = count;
gridColors = new int[mCount];
Arrays.fill(gridColors,Color.LTGRAY);
}


public int getCount() {
return mCount;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;

if (convertView == null) {
// if it's not recycled, initialize some attributes
textView = new TextView(mContext);
textView.setGravity(Gravity.CENTER);
textView.setLayoutParams(new GridView.LayoutParams(100, 100));

} else {
textView = (TextView) convertView;
}


textView.setBackgroundColor(gridColors[position]);

textView.setText("" + position);
return textView;
}

public void setPositionColor(int position, int color) {
gridColors[position] = color;
notifyDataSetChanged();
}

}

activity_main.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"
android:id="@+id/relativeLayout"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/inputLayout"
android:layout_alignParentTop="true"
android:background="#a9a9a9"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:id="@+id/textView"
android:text="All primes less than\n or equal to this number:"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />



<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true" android:focusableInTouchMode="true"
android:layout_width="0px" android:layout_height="0px"
android:inputType="numberSigned"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->

<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext" android:nextFocusLeft="@id/autotext"
android:inputType="numberSigned" android:imeOptions="actionDone"
android:layout_gravity="center_vertical" android:gravity="center_vertical"
android:layout_centerVertical="true" android:layout_toEndOf="@+id/textView"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:inputType="numberSigned"
android:ems="10"
android:id="@+id/inputEditText"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/autotext"
android:layout_toEndOf="@+id/textView" />

</RelativeLayout>


<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inputLayout"

android:id="@+id/title_horizontalScrollView"
android:fillViewport="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/dataLayout"
>

<GridView
android:id="@+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_alignParentBottom="true" />

</RelativeLayout>

</HorizontalScrollView>

</RelativeLayout>

menu_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_status"
android:icon="@drawable/ic_action_play"
android:title="Play"
app:showAsAction="always"/>
<item android:id="@+id/action_stop"
android:icon="@drawable/ic_action_stop"
android:title="Stop"
app:showAsAction="always"/>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />

</menu>

最佳答案

最终,您应该切换到将 RecyclerView 与 GridLayoutManager 结合使用。 RecyclerView 提供了非常灵活的方法来更新 UI。在您的情况下,notifyItemChanged(int position) 将有效地更新一个 TextView。了解更多 here .

关于java - android 在 GridView 更新之间等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30161458/

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