我知道这个问题已经被提出,但是我没有任何适当的解决办法来解决0职位的问题。我正在Gridview上工作,并为此创建了一个适配器(ButtonAdapter)。因此,我有4个按钮,如果我单击其中任何一个按钮,则所选按钮应更改bg颜色,其余三个按钮应均具有降低的不透明度,可以说0.3,但是我的问题是我的第一个按钮不会更改alpha。那么确切的问题是什么?
程序包适配器;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import com.appsee.Appsee;
import com.regiumconsulting.donationssumup.MainActivity;
import com.regiumconsulting.donationssumup.R;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
public class ButtonAdapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
private BigDecimal amnt;
private Button[] chosenone = new Button[1];
private Button[] tempBtn = null;
private BigDecimal amount;
private Map<Button, BigDecimal> buttons = new HashMap<>();
public ButtonAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
tempBtn = new Button[mobileValues.length];
}
public View getView(final int position, View convertView, ViewGroup parent) {
View gridView = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = new View(context);
// get layout from item_adaptor.xmlor.xml
gridView = inflater.inflate(R.layout.item_adaptor, null);
} else {
gridView = convertView;
}
// set image based on selected text
final Button btnView = (Button) gridView
.findViewById(R.id.grid_item_image);
setBtnAmnt(position, btnView);
btnView.setBackgroundResource(R.drawable.button_bg_dashed_border);
tempBtn[position] = btnView;
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < tempBtn.length; i++) {//set alpha to 0.5 for all buttons
tempBtn[i].setAlpha(0.3f);//updating only three buttons bg not for 1st one.
}
chosenone[0] = btnView;
amount = buttons.get(chosenone[0]);
chosenone[0].setBackgroundResource(R.drawable.button_bg_border);//set alpha 1 to selected one and set selected color
chosenone[0].setAlpha(1f);
}
}
});
return gridView;
}
@Override
public int getCount() {
return mobileValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
private void setBtnAmnt(int position, Button imageView) {
amnt = new BigDecimal(mobileValues[position]);
imageView.setText(String.format("£ %s", amnt.toString()));
buttons.put(imageView, amnt);
}
}
Also, I have created on method inside main activity let say screenClick.
public void screenClick() {
gridView.setAdapter(adaptor);
adaptor.notifyDataSetChanged();
}
So If I call this method and then perform the same operation as mentioned above then only it will work and it will set 0.3 alpha for a 1st button. So what is the issue I don't understand?
I have also attached a screenshot link where a 1st button doesn't have reduced opacity.
https://imgur.com/a/bofGMk4