我正在使用列表视图,当我在列表视图中选择行时,它被选中它的工作正常。但是我希望列表行保持突出显示,即使在listview中选择多行也是如此。
Giftafriend.java
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class Giftafriend extends android.support.v4.app.Fragment implements View.OnClickListener {
ListView list;
String[] product;
String[] price;
int[] image;
TextView total;
TableLayout stk;
private Giftlist adapter;
ArrayList<Gift> arraylist = new ArrayList<Gift>();
ArrayList<Gift> selected = new ArrayList<Gift>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int amount=0;
View view = inflater.inflate(R.layout.fragment_giftafriend, container, false);
product = new String[]{"Hair", "MSpa", "Style", "Style",
"Hair", "Spa", "Spa", "Style", "HaIR", "hAIR"};
price = new String[]{"100", "200", "400", "150",
"200", "320", "400", "150", "200", "150"};
image = new int[]{R.drawable.coloring, R.drawable.style, R.drawable.spa, R.drawable.straitning, R.drawable.treatment,
R.drawable.spa, R.drawable.straitning, R.drawable.treatment, R.drawable.coloring, R.drawable.style};
// Locate the ListView in listview_main.xml
total=(TextView) view.findViewById(R.id.total);
list = (ListView) view.findViewById(R.id.list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.setSelectedPosition(position);
adapter.notifyDataSetChanged();
}
});
for (int i = 0; i < product.length; i++) {
Gift wp = new Gift(product[i], image[i], price[i]);
arraylist.add(wp);
}
adapter = new Giftlist(getContext(), arraylist);
list.setAdapter(adapter);
getSupportActionBar().setDisplayOptions(android.app.ActionBar.DISPLAY_SHOW_HOME | android.app.ActionBar.DISPLAY_USE_LOGO);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.giftactionbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
return view;
}
private ActionBar getSupportActionBar() {
return ((AppCompatActivity) getActivity()).getSupportActionBar();
}
int amount=0;
public void showAdd(View view)
{
int position = list.getPositionForView(view);
Gift gift = adapter.getItem(position);
amount = amount + Integer.parseInt(gift.getPrice());
total.setText(String.valueOf(amount));
}
@Override
public void onClick(View v) {
}}
Giftlist.java
import java.util.ArrayList;
import android.graphics.Color;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Giftlist extends BaseAdapter {
Context mContext;
private int counter = 0;
LayoutInflater inflater;
private java.util.List<Gift> rest=null;
private int selectedPosition = 1;
private ArrayList<Gift> arraylist=new ArrayList<Gift>();
LinearLayout listrow;
public Giftlist(Context context,
java.util.List<Gift> rest) {
mContext = context;
this.rest = rest;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<Gift>();
this.arraylist.addAll(rest);
}
@Override
public int getCount() {
return rest.size();
}
@Override
public Gift getItem(int position) {
return rest.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
View view1;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view1 = new View(mContext);
view1 = inflater.inflate(R.layout.giftlist, null);
} else {
view1 = (View)view;
}
TextView product = (TextView) view1.findViewById(R.id.product);
final TextView price = (TextView) view1.findViewById(R.id.price);
ImageView dish = (ImageView) view1.findViewById(R.id.image);
listrow=(LinearLayout) view1.findViewById(R.id.listrow);
product.setText(rest.get(position).getProduct());
price.setText(rest.get(position).getPrice());
dish.setImageResource(rest.get(position).getImage());
if (position == selectedPosition) {
listrow.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
listrow.setSelected(true);
}
else
listrow.setBackgroundColor(Color.TRANSPARENT);
return view1;
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
}}
答案 0 :(得分:0)
您可以使用SharedPreference并存储要保存的列表视图项目的位置以进行多选。
答案 1 :(得分:0)
如果要选择多个项目,请将listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
设置为listView;如果不想选择,则将listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
设置为<{p>}。
点击此处http://jax-ws.java.net/或Highlight custom listview item when long click
编辑:
您还需要将background
行ListView
行设置为选择器资源,例如bg_list_item.xml
<item android:drawable="@drawable/selected"
android:state_checked="true" />
<item android:drawable="@drawable/not_selected" />
这只是一个概述,有关完整实施,请参阅提供的链接。