gpt4 book ai didi

java - 如何使用 viewHolder 模式让 UI 元素在 listView 中滚动?

转载 作者:行者123 更新时间:2023-12-01 09:04:14 27 4
gpt4 key购买 nike

我正在研究使用 viewHolder 模式来使我的 UI 元素在 ListView 滚动时不会重置为默认值。调查之后我不知道该怎么做。我什至不确定我现有的代码是否允许我使用 viewHolder 模式。我是初学者,所以请详细说明,不要跳过细节。谢谢

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ArrayList<String> Chores = new ArrayList<>();
Chores.add("");

final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
listViewObject.setAdapter(MyAdapter);

listViewObject.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String ChoreString = String.valueOf(parent.getItemAtPosition(position));
}
}

);
final Button button = (Button) findViewById(R.id.button_ID);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Chores.add("");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
}
});
}
}

自定义适配器.java

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import static com.example.emilythacker.chorelist.R.id.textView_ID;

class CustomAdapter extends ArrayAdapter{

public CustomAdapter(Context context, ArrayList choreText) {
super(context, R.layout.custon_listview_row, choreText);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);

ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
final TextView textView = (TextView) customView.findViewById(textView_ID);

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//what happens when textView is clicked
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
final EditText input = new EditText(getContext());
input.setHint("hint");
alertDialog.setView(input);
alertDialog.setTitle("Set Chore");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

//this will set text to "hello" only if user does not enter anything into input
//textView.setText("hello");

//this also will only work if there is no input entered into input...wich will change textView into empty space
textView.setText(input.getText().toString());

//works the same with or without dialog.dismiss();
dialog.dismiss();
}
});
alertDialog.show();
}
});
imageButton.setImageResource(R.drawable.clock);
return customView;
}
}

最佳答案

我找到了this教程在学习使用 ViewHolder 模式时非常有帮助。它在解释如何使用它方面非常清楚,但如果您感到困惑,请随时询问。

关于java - 如何使用 viewHolder 模式让 UI 元素在 listView 中滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41418944/

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