gpt4 book ai didi

java - 取消单击 ListView 元素

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

我正在创建一个 ListView ,人们可以在阅读时在 ListView 上勾选书籍。当他们选择一本书时,该行的 alpha 变为 0.2f,并且会出现一条提示,告诉他们已阅读该书。我试图做到这一点,以便当选择一本书时,他们也可以通过再次单击它来取消选择它,这将使 alpha 回到 1f。

我在 onItemClick 方法中创建了一个 if else 循环,该循环允许我取消选择单击的行,但是它遵循的模式是第一次单击变为 0.2f,第二次单击变为 1f,第三次单击变为 0.2f等等。相反,我希望只有 0.2f 处的行能够转到 1f,反之亦然。

这是我的代码:

public class Books extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ListView booksListView = (ListView)findViewById(R.id.booksListView);

final ArrayList<String> topBooks = new ArrayList<String>(asList("1984", "To Kill a Mockingbird", "Pride and Prejudice",
"Harry Potter and the Sorcerer's Stone", "The Great Gatsby","Jane Eyre","Wuthering Heights","The Catcher in the Rye",
"The Hobbit","Brave New World","The Lord of the Rings: The Fellowship of the Ring","Don Quixote",
"Catch-22","The Count of Monte Cristo","Harry Potter and the Goblet of Fire","The Grapes of Wrath","The Adventures of Huckleberry Finn",
"The Diary of a Young Girl","Gone with the Wind","Harry Potter and the Deathly Hallows","Moby Dick","Harry Potter and the Half-Blood Prince","War and Peace",
"Animal Farm","Anna Karenina","Ulysses","Lord of the Flies","The Divine Comedy","One Hundred Years of Solitude","Frankenstein","The Perks of Being a Wallflower",
"The Fault in Our Stars","Good to Great","Harry Potter and the Chamber of Secrets","Harry Potter and the Order of the Phoenix","Harry Potter and the Prisoner of Azkaban",
"The Amazing Adventures of Kavalier & Clay","The Hunger Games","The Lord of the Rings: The Two Towers","The Lord of the Rings: The Return of the King", "The Maze Runner",
"Looking for Alaska","Fahrenheit 451", "Hamlet","Gullivers Travels","The Canterbury Tales","Rebecca","The Brothers Karamazov","Lover Awakened","At Grave's End"));

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topBooks);

booksListView.setAdapter(arrayAdapter);

final MediaPlayer mPlayer = MediaPlayer.create(this,R.raw.pindrop);

booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

if(view.getAlpha()==1f) {

Toast.makeText(getApplicationContext(), "You Read " + topBooks.get(position), Toast.LENGTH_LONG).show();
view.animate().alpha(0.2f);
mPlayer.start();
}

else {

view.animate().alpha(1f);

}
}
});
}

}

最佳答案

根据您的代码,考虑其他两个答案,创建一个 BookAdapter、一个 BookModel,然后为图书行创建 xml:

书籍行的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="72dp">

<TextView
android:id="@+id/title"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

图书模型:

public class BookModel {

public String name;
public boolean isRead;

}

BookAdapter:

public class BookAdapter extends BaseAdapter {

private Context context;
private List<BookModel> books;

static class Holder{
TextView mTvName;
}

public BookAdapter(Context context, List<BookModel> books){
this.context = context;
this.books = books;
}

@Override
public int getCount() {
return books.size();
}

@Override
public Object getItem(int position) {
return books.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

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

if(convertView == null){
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_row, parent, false);

holder = new Holder();

holder.mTvName = (TextView) convertView.findViewById(R.id.title);

convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}

final BookModel bookModel = books.get(position);

if(bookModel != null){

holder.mTvName.setText(bookModel.name);

if(bookModel.isRead){
holder.mTvName.animate().alpha(0.2f);
bookModel.isRead = true;
}else{
holder.mTvName.animate().alpha(1f);
bookModel.isRead = false;
}
}

return convertView;
}
}

图书 Activity 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.divshark.booksample.Books">
<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/ThemeOverlay.AppCompat.Light"/>

<ListView
android:id="@+id/booksListView"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:layout_below="@+id/toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>

然后实现您的 Activity 编辑 - 现在书籍阅读会保留在首选项中:

public class Books extends AppCompatActivity {

public List<BookModel> books;
private SharedPreferences mSharedPreferences;
private static final String SHARED_PREFERENCES = "SharedPrefs";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_books);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mSharedPreferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);

ListView booksListView = (ListView)findViewById(R.id.booksListView);

books = createBooks();


final BookAdapter adapter = new BookAdapter(this, books);

booksListView.setAdapter(adapter);

final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.pindrop);

booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

final BookModel bookModel = books.get(position);
if (!bookModel.isRead) {

Toast.makeText(getApplicationContext(), "You Read " + bookModel.name, Toast.LENGTH_LONG).show();
view.animate().alpha(0.2f);
mPlayer.start();
bookModel.isRead = true;

if(mSharedPreferences != null){
mSharedPreferences.edit().putBoolean(bookModel.name, bookModel.isRead).apply();
}

adapter.notifyDataSetChanged();
} else {

view.animate().alpha(1f);
mPlayer.start();
bookModel.isRead = false;

if(mSharedPreferences != null){
mSharedPreferences.edit().putBoolean(bookModel.name, bookModel.isRead).apply();
}

adapter.notifyDataSetChanged();
}
}
});
}

/**
* Creates an ArrayList<BookModel>
* @return - List<BookModel>
*/
public List<BookModel> createBooks(){

final ArrayList<String> topBooks = new ArrayList<String>(Arrays.asList("1984", "To Kill a Mockingbird", "Pride and Prejudice",
"Harry Potter and the Sorcerer's Stone", "The Great Gatsby", "Jane Eyre", "Wuthering Heights", "The Catcher in the Rye",
"The Hobbit", "Brave New World", "The Lord of the Rings: The Fellowship of the Ring", "Don Quixote",
"Catch-22", "The Count of Monte Cristo", "Harry Potter and the Goblet of Fire", "The Grapes of Wrath", "The Adventures of Huckleberry Finn",
"The Diary of a Young Girl", "Gone with the Wind", "Harry Potter and the Deathly Hallows", "Moby Dick", "Harry Potter and the Half-Blood Prince", "War and Peace",
"Animal Farm", "Anna Karenina", "Ulysses", "Lord of the Flies", "The Divine Comedy", "One Hundred Years of Solitude", "Frankenstein", "The Perks of Being a Wallflower",
"The Fault in Our Stars", "Good to Great", "Harry Potter and the Chamber of Secrets", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Prisoner of Azkaban",
"The Amazing Adventures of Kavalier & Clay", "The Hunger Games", "The Lord of the Rings: The Two Towers", "The Lord of the Rings: The Return of the King", "The Maze Runner",
"Looking for Alaska", "Fahrenheit 451", "Hamlet", "Gullivers Travels", "The Canterbury Tales", "Rebecca", "The Brothers Karamazov", "Lover Awakened", "At Grave's End"));

List<BookModel> books = new ArrayList<>(topBooks.size());

for(int i = 0; i < topBooks.size(); i++){
BookModel bookModel = new BookModel();
bookModel.name = topBooks.get(i);

if(mSharedPreferences != null) {
// sets the IsRead field from value in preferences
bookModel.isRead = mSharedPreferences.getBoolean(bookModel.name, false);
}else {

bookModel.isRead = false;

// Open preferences to write the value in on load
mSharedPreferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);

// Stores the book in shared preferences as un-read
mSharedPreferences.edit().putBoolean(bookModel.name, false).apply();
}

books.add(bookModel);
}

return books;
}
}

和样式以防万一您需要它们:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

祝你好运,编码愉快!

关于java - 取消单击 ListView 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35296055/

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