[英]How to make Section ListView Header unclickable?
我有一個節列表視圖,並且有一個行和標題。 標頭顯示月份和年份,其中行顯示利潤和物料。 我為“行”創建了一個對話框,但是每當我單擊“標題”時,都會提示我錯誤。 如何使標題不可點擊?
我的適配器類
public class TransactionAdapter extends BaseAdapter {
ArrayList<Object> transactions;
Context c;
LayoutInflater inflater;
static final int ROW = 0;
static final int HEADER = 1;
public TransactionAdapter(Context c, ArrayList<Object> transactions){
this.c = c;
this.transactions = transactions;
}
//Get size of the Transaction ArrayList
@Override
public int getCount() {
return transactions.size();
}
//Get single transaction from the Transaction ArrayList
@Override
public Object getItem(int position) {
return transactions.get(position);
}
//Get Single transaction identifier from the Transaction ArrayList
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position){
//Check the current transaction is Transaction
if(getItem(position) instanceof ProfitTransactions){
return ROW;
}
return HEADER;
}
@Override
public int getViewTypeCount(){
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Type of View which is ROW(0) or HEADER(1)
int type = getItemViewType(position);
//If there is no View create it,
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView = inflater.inflate(R.layout.activity_transaction_items, null);
break;
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
default:
break;
}
}
switch (type){
case ROW:
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
TextView tvDay = (TextView)convertView.findViewById(R.id.day);
TextView tvTID = (TextView)convertView.findViewById(R.id.tID);
TextView tvTotalPrice = (TextView)convertView.findViewById(R.id.totalPrice);
TextView tvTimeOrder = (TextView)convertView.findViewById(R.id.tvTime);
Log.d("transadapter","-----Test: " + Integer.parseInt(transaction.getDayOfOrder()));
tvDay.setText(transaction.getDayOfOrder()+ getDayNumberSuffix(Integer.parseInt(transaction.getDayOfOrder())));
tvTID.setText("TID: " + transaction.gettId());
tvTotalPrice.setText("+$"+String.format("%.2f", transaction.getTotalPrice()));
tvTimeOrder.setText("At: " + transaction.getTimeOfOrder());
break;
case HEADER:
String header = (String)getItem(position);
// from string to date
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/yyyy");
Date date = null;
try {
date = inputFormat.parse(header);
} catch (ParseException e) {
e.printStackTrace();
}
// from date to string
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM yyyy");
String dateTime = outputFormat.format(date);
TextView tvMonthYear = (TextView)convertView.findViewById(R.id.tvHeaderMonthYear);
tvMonthYear.setText(dateTime);
default:
break;
}
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
//Create the Dialog Box
AlertDialog.Builder builder = new AlertDialog.Builder(c);
//Put message in the Dialog Box
builder.setMessage("Name: " + transaction.getName() + "\n" +
"Price: " + transaction.getPrice() + "\n" +
"Quantity: " + transaction.getQuantity() + "\n" +
"Total Price: " + transaction.getTotalPrice() + "\n"
)
//If user click Yes
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//Show the dialog after creating
AlertDialog dialog = builder.show();
}
});
return convertView;
}
}
我想要的預期結果是我希望包含月份和年份的標題不可單擊,並且不會響應任何內容。
與決定要填充的內容以及如何在getView
方法中填充視圖的方式相同,可以將所需類型的OnClickListener
設置為null。 您也可以使用setClickable
方法。
在getViewMethod內部使用此
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
convertView.setEnabled(false);
在convertView.setOnClickListener之前,添加if邏輯,以檢查convertView是標題還是所選的行! 喜歡-
if(!convertView.equals("HEADER"))
{
your onClick listener.....
}
else
{
what you want to do if its headerview being the view selected...
}
當前,您正在每個視圖上注冊點擊偵聽器。 您只想將其分配給行,而不是標題,因為在處理程序中,您試圖訪問僅存在於行上的ProcessTransaction
上的字段。
因此,只需將您的點擊偵聽器代碼拉到交換機的ROW
分支中即可。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Type of View which is ROW(0) or HEADER(1)
int type = getItemViewType(position);
//If there is no View create it,
if (convertView == null) {
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (type) {
case ROW:
convertView = inflater.inflate(R.layout.activity_transaction_items, null);
break;
case HEADER:
convertView = inflater.inflate(R.layout.activity_transaction_header, null);
convertView.setBackgroundColor(Color.rgb(220,220,220));
default:
break;
}
}
switch (type){
case ROW:
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
TextView tvDay = (TextView)convertView.findViewById(R.id.day);
TextView tvTID = (TextView)convertView.findViewById(R.id.tID);
TextView tvTotalPrice = (TextView)convertView.findViewById(R.id.totalPrice);
TextView tvTimeOrder = (TextView)convertView.findViewById(R.id.tvTime);
Log.d("transadapter","-----Test: " + Integer.parseInt(transaction.getDayOfOrder()));
tvDay.setText(transaction.getDayOfOrder()+ getDayNumberSuffix(Integer.parseInt(transaction.getDayOfOrder())));
tvTID.setText("TID: " + transaction.gettId());
tvTotalPrice.setText("+$"+String.format("%.2f", transaction.getTotalPrice()));
tvTimeOrder.setText("At: " + transaction.getTimeOfOrder());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProfitTransactions transaction = (ProfitTransactions)getItem(position);
//Create the Dialog Box
AlertDialog.Builder builder = new AlertDialog.Builder(c);
//Put message in the Dialog Box
builder.setMessage("Name: " + transaction.getName() + "\n" +
"Price: " + transaction.getPrice() + "\n" +
"Quantity: " + transaction.getQuantity() + "\n" +
"Total Price: " + transaction.getTotalPrice() + "\n"
)
//If user click Yes
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
//Show the dialog after creating
AlertDialog dialog = builder.show();
}
});
break;
case HEADER:
String header = (String)getItem(position);
// from string to date
SimpleDateFormat inputFormat = new SimpleDateFormat("MM/yyyy");
Date date = null;
try {
date = inputFormat.parse(header);
} catch (ParseException e) {
e.printStackTrace();
}
// from date to string
SimpleDateFormat outputFormat = new SimpleDateFormat("MMM yyyy");
String dateTime = outputFormat.format(date);
TextView tvMonthYear = (TextView)convertView.findViewById(R.id.tvHeaderMonthYear);
tvMonthYear.setText(dateTime);
default:
break;
}
return convertView;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.