- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在更改位置上的 GroupView 背景颜色时遇到问题..根据我的代码当我按第一个数字 GroupView 时,第一个数字 GroupView 的颜色会发生变化。
并且
当我按第二个数字 GroupView 时,第一个数字 GroupView 的颜色就会改变。
我想更改帖子展开和折叠时的 GroupView 颜色。
<小时/>ExpandableListViewAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.NumberPicker;
import android.widget.TextView;
import com.acase.clouds.cloudstailor.Models.ExpandableModel;
import com.acase.clouds.cloudstailor.R;
import java.util.List;
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private LayoutInflater inflater;
private List<ExpandableModel> listDataGroup;
public ExpandableListViewAdapter(Context context, List<ExpandableModel> listDataGroup) {
this.context = context;
this.listDataGroup = listDataGroup;
this.inflater = LayoutInflater.from(context);
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return listDataGroup.get(groupPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 1;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ExpandableModel model = listDataGroup.get(groupPosition);
if (convertView == null) {
convertView = inflater.inflate(model.getLayoutId(), null);
}
switch (model.getType()) {
case AGE:
setNumberPicker(convertView, model);
break;
case STATE:
setNumberPicker(convertView, model);
break;
}
return convertView;
}
private void setNumberPicker(View convertView, final ExpandableModel model) {
NumberPicker numberPicker = convertView.findViewById(R.id.numberPicker);
numberPicker.setMinValue(0);
numberPicker.setMaxValue(99);
if (!model.getValue().isEmpty()) {
numberPicker.setValue(Integer.parseInt(model.getValue()));
}
// to change formate of number in numberpicker
numberPicker.setFormatter(new NumberPicker.Formatter() {
@Override
public String format(int i) {
return String.format("%02d", i);
}
});
numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
model.setValue(i1 + "");
}
});
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return this.listDataGroup.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.listDataGroup.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row_group, null);
}
ExpandableModel model = listDataGroup.get(groupPosition);
TextView tv_title = convertView.findViewById(R.id.tv_title);
TextView tv_value = convertView.findViewById(R.id.tv_value);
tv_title.setText(model.getName());
tv_value.setText(model.getValue());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
编辑配置文件 Activity
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.acase.clouds.cloudstailor.Adapter.ExpandableListViewAdapter;
import com.acase.clouds.cloudstailor.Models.ExpandableModel;
import com.bumptech.glide.Glide;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class EditProfileActivity extends AppCompatActivity {
Toolbar toolbar;
ImageView profileimage;
TextView changephototext, textView;
RelativeLayout item;
private static final int PICK_IMAGE_REQUEST = 1;
public Uri path;
private ExpandableListView expandableListView;
private ExpandableListViewAdapter expandableListViewAdapter;
List<ExpandableModel> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
toolbar = findViewById(R.id.tool).findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Edit Profile");
profileimage = findViewById(R.id.Profile_Img);
profileimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fileChooser();
}
});
changephototext = findViewById(R.id.Change_Photo_Button);
changephototext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
fileChooser();
}
});
// initializing the views
initViews();
// initializing the listeners
initListeners();
// initializing the objects
initObjects();
}
private void initViews() {
expandableListView = findViewById(R.id.expandableListView);
}
private void initListeners() {
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
ExpandableModel model = list.get(groupPosition);
Toast.makeText(EditProfileActivity.this, model.getName() + " clicked at " + childPosition, Toast.LENGTH_SHORT).show();
return false;
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
ExpandableModel model = list.get(groupPosition);
Toast.makeText(EditProfileActivity.this, model.getName() + " expanded", Toast.LENGTH_SHORT).show();
item = findViewById(R.id.Group);
item.setBackgroundResource(R.drawable.selectedlistback);
textView = findViewById(R.id.tv_title);
textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
}
});
// ExpandableListView Group collapsed listener
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
ExpandableModel model = list.get(groupPosition);
Toast.makeText(EditProfileActivity.this, model.getName() + " collapsed", Toast.LENGTH_SHORT).show();
item = findViewById(R.id.Group);
item.setBackgroundResource(R.drawable.listback);
textView = findViewById(R.id.tv_title);
textView.setTextColor(getResources().getColor(R.color.colorAccent));
}
});
}
private void initObjects() {
ExpandableModel age = new ExpandableModel("AGE", "", ExpandableModel.ExpandableType.AGE, R.layout.list_row_child);
list.add(age);
ExpandableModel state = new ExpandableModel("STATE", "", ExpandableModel.ExpandableType.STATE, R.layout.list_row_child);
list.add(state);
expandableListViewAdapter = new ExpandableListViewAdapter(this, list);
expandableListView.setAdapter(expandableListViewAdapter);
}
private void fileChooser()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data.getData() != null)
{
path = data.getData();
Glide.with(this).load(path).into(profileimage);
path = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
profileimage.setImageBitmap(bitmap);
}catch (IOException e){
e.printStackTrace();
}
}
}
}
最佳答案
尝试在适配器的 getGroupView 内添加以下代码:
if(isExpanded) {
convertView.setBackgroundResource(R.drawable.selectedlistback);
tv_title.setTextColor(context.getResources().getColor(R.color.colorPrimaryDark));
}else{
convertView.setBackgroundResource(R.drawable.listback);
tv_title.setTextColor(context.getResources().getColor(R.color.colorAccent));
}
并删除监听器。希望有帮助!
关于java - 如何在 ExpandableListView 中更改 GroupView 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222311/
我的 Activity 中有以下适配器。我在 GroupView 上设置了一个 CheckBox,当触摸它时将选中/取消选中 subview (它也有一个 CheckBox)。 CheckBox(tr
这是我的代码: Typeface font = Typeface.createFromAsset(getAssets(), "fonts/choco.ttf"); ViewGroup
我在我的应用程序中使用了 ExpandableListView,一切正常。虽然,每个 groupView 都包含大量子项,并且当展开 groupView 并显示子项时,我希望 groupView 在向
我在更改位置上的 GroupView 背景颜色时遇到问题..根据我的代码当我按第一个数字 GroupView 时,第一个数字 GroupView 的颜色会发生变化。 并且 当我按第二个数字 Group
在我的项目中,可扩展 ListView 对所有登录都是通用的,但在新的更新中,基于登录用户的角色,我想停止显示某些组 View 的 subview 。我设法设置了一个“拒绝访问”对话框,但我无法阻止组
在德尔福 2009 中: 当 TListView 的 GroupView 处于事件状态时,向 TListView 添加或插入项目总是将其添加到列表的末尾,无论参数指定的索引如何。当 GroupView
我之前没有注意到这一点,但当我在我的应用程序中上下滚动时,复选框会随机选中和取消选中,这让我感到很愚蠢。复选框位于 GroupView 而不是 Expandable ListView 的子元素上。我对
好吧,我是 recyclerview 的新手,我在这里迷路了。我想在一个 Activity 中有 2 个(或更多)recyclerview。我的问题出在这部分代码中 error: here i
我正在尝试访问 GroupView 的 TextView ,它显示了 ChildView 中所选复选框的计数。 例如 - North 是 GroupView ,下面带有复选框的列表是 ChildVie
我是一名优秀的程序员,十分优秀!