我的RelativeLayout
中有一些按钮,但是当长按按钮(0)时,@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (resultCode == RESULT_OK && requestCode == 1 && data != null) {
PreferenceManager.getDefaultSharedPreferences(this).edit().putString(App.IMAGE_URI, data.getData().toString()).apply();
Picasso.with(this).load(data.getData()).centerCrop().resize(width, height).into(target);
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("IMAGE", true).apply();
}
}
会打开gallary,以便用户可以选择图像作为{{1}的背景}。选择图像后,我希望该按钮(0)更改其Alpha以指示选择了图像。我该如何存档?
所选图片将在我的MainActivity中重新审核,如下所示:
@Override
public boolean onLongClick(View v) {
if (getAdapterPosition() == 0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Intent.createChooser(intent, "Choose a background image");
((Activity) context).startActivityForResult(intent, GET_IMG_REQ);
}
此意图代码位于适配器中:
Resources/Private/Partials/List/Item.html
答案 0 :(得分:0)
我认为简单的方法是在适配器中创建idClick
之类的变量,之后您可以通过活动控制适配器获取idClick
。
<强>适配器强>
public class FakeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<View> headers = new ArrayList<>();
List<View> footers = new ArrayList<>();
public static final int TYPE_HEADER = 111;
public static final int TYPE_FOOTER = 222;
public static final int TYPE_ITEM = 333;
private List<Item> items;
private Activity context;
private LayoutInflater mInflater;
public int idClick = -1;
public FakeAdapter(List<Item> items, Activity context) {
this.context = context;
mInflater = LayoutInflater.from(context);
if (items == null) {
throw new NullPointerException(
"items must not be null");
}
this.items = items;
}
public void clear() {
if (items != null) {
items.clear();
notifyDataSetChanged();
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.view_item_fake,
parent,
false);
return new QuestionHolder(itemView);
} else {
//create a new framelayout, or inflate from a resource
FrameLayout frameLayout = new FrameLayout(parent.getContext());
//make sure it fills the space
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return new HeaderFooterViewHolder(frameLayout);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//check what type of view our position is
if (position < headers.size()) {
View v = headers.get(position);
//add our view to a header view and display it
bindHeaderFooter((HeaderFooterViewHolder) holder, v);
} else if (position >= headers.size() + items.size()) {
View v = footers.get(position - items.size() - headers.size());
//add oru view to a footer view and display it
bindHeaderFooter((HeaderFooterViewHolder) holder, v);
} else {
//it's one of our items, display as required
bindHolder((QuestionHolder) holder, position - headers.size());
}
}
private void bindHeaderFooter(HeaderFooterViewHolder vh, View view) {
//empty out our FrameLayout and replace with our header/footer
vh.base.removeAllViews();
vh.base.addView(view);
}
private void bindHolder(final QuestionHolder holder, final int position) {
final Item item = getItem(position);
if (item != null) {
holder.button.setText(item.getTitle());
}
if (TextUtils.isEmpty(item.getPathFromGallery())) {
holder.button.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_bright));
} else {
holder.button.setBackgroundColor(context.getResources().getColor(android.R.color.white));
}
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
idClick = position;
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
context.startActivityForResult(galleryIntent, RecyclerListActivity.REUQUEST_LOAD_IMAGE);
}
});
}
@Override
public int getItemCount() {
return headers.size() + items.size() + footers.size();
}
public Item getItem(int position) {
if (position < 0 || position >= items.size()) {
return null;
}
return items.get(position);
}
public final static class QuestionHolder extends RecyclerView.ViewHolder {
@Bind(R.id.header_text)
Button button;
public QuestionHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
final static class HeaderHolder extends RecyclerView.ViewHolder {
//@Bind(R.id.header_merchant)
public TextView header;
public HeaderHolder(View itemView) {
super(itemView);
//ButterKnife.bind(this, itemView);
header = (TextView) itemView;
}
}
@Override
public int getItemViewType(int position) {
//check what type our position is, based on the assumption that the order is headers > items > footers
if (position < headers.size()) {
return TYPE_HEADER;
} else if (position >= headers.size() + items.size()) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
//add a header to the adapter
public void addHeader(View header) {
if (header != null && !headers.contains(header)) {
headers.add(header);
//animate
notifyItemInserted(headers.size() - 1);
}
}
//remove a header from the adapter
public void removeHeader(View header) {
if (header != null && headers.contains(header)) {
//animate
notifyItemRemoved(headers.indexOf(header));
headers.remove(header);
if (header.getParent() != null) {
((ViewGroup) header.getParent()).removeView(header);
}
}
}
//add a footer to the adapter
public void addFooter(View footer) {
if (footer != null && !footers.contains(footer)) {
footers.add(footer);
//animate
notifyItemInserted(headers.size() + items.size() + footers.size() - 1);
}
}
//remove a footer from the adapter
public void removeFooter(View footer) {
if (footer != null && footers.contains(footer)) {
//animate
notifyItemRemoved(headers.size() + items.size() + footers.indexOf(footer));
footers.remove(footer);
if (footer.getParent() != null) {
((ViewGroup) footer.getParent()).removeView(footer);
}
}
}
//our header/footer RecyclerView.ViewHolder is just a FrameLayout
public static class HeaderFooterViewHolder extends RecyclerView.ViewHolder {
FrameLayout base;
public HeaderFooterViewHolder(View itemView) {
super(itemView);
this.base = (FrameLayout) itemView;
}
}
}
<强>物品强>
public class Item {
private String title;
private String pathFromGallery;
public Item(String title, String pathFromGallery) {
this.title = title;
this.pathFromGallery = pathFromGallery;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPathFromGallery() {
return pathFromGallery;
}
public void setPathFromGallery(String pathFromGallery) {
this.pathFromGallery = pathFromGallery;
}
}`
活动
public class RecyclerListActivity extends AppCompatActivity {
@Bind(R.id.srl)
SwipeRefreshLayout refreshLayout;
@Bind(R.id.recyclerView)
RecyclerView recyclerView;
FakeAdapter adapter;
List<Item> lsItem;
public static int REUQUEST_LOAD_IMAGE = 1111;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
setAdapter();
}
private void setAdapter() {
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
if (adapter == null) {
adapter = new FakeAdapter(getLsItem(), this);
recyclerView.setAdapter(adapter);
}
}
private List<Item> getLsItem() {
lsItem = new ArrayList<>();
lsItem.add(new Item("Button 1", ""));
lsItem.add(new Item("Button 2", ""));
lsItem.add(new Item("Button 3", ""));
lsItem.add(new Item("Button 4", ""));
return lsItem;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == REUQUEST_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
updateAdapter(imgDecodableString);
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
private void updateAdapter(String path) {
if(adapter.idClick != -1) {
lsItem.get(adapter.idClick).setPathFromGallery(path);
adapter.notifyDataSetChanged();
}
}
}
view_item_fake.xml
只有一个按钮。这是你想要的答案,因为我没有看到你适配器。我刚刚根据我的旧项目修改了我的代码(命名也许你感到困惑),在FakeAdapter
中你只需要查看方法bindHolder
。
答案 1 :(得分:0)
如果我正确理解您的问题,主要问题是告诉适配器图像是否可用,并使{_ 1}}项中包含button_0,alpha更改。
RecyclerView
方法RecyclerView.Adapter's
。如果被调用,则重新绑定特定项目(调用notifyItemChanged(int position, Object payload)
)。在有效负载中,您可以指定项目“刷新”的附加内容。让我们在你的场景中使用它:
适配器的实施:
onBindViewHolder(VH holder, int position, List<Object> payload)
Activity类中的用法:
// Helper class to indicate that we should decrease provided item's button alpha
public class DecreaseAlpha { }
@Override public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
if (!payloads.isEmpty() && payloads.get(0) instanceof DecreaseAlpha) {
holder.your_button.setAlpha(0.5f);
} else {
// make sure to call classic version of onBindViewHolder when payload is not available
onBindViewHolder(holder, position);
}
}