所以我有一个包含ViewPager的Activity,它包含一些片段,包括一个带有RecyclerView的片段。您可以单击RecyclerView上的其中一个项目来编辑其所有详细信息,这可以通过启动第二个活动来完成,该活动包含特定于该项目的可编辑字段。
但是,假设我改变了项目的名称。现在我按Save或后退按钮或其他任何关闭此活动并返回第一个活动。由于名称现在可能不同,因此RecyclerView也应该更新项目名称。
但是如何检测此事件?我如何基本上“在关闭第二个Activity时,在第一个Activity的ViewPager中的片段中的RecyclerView中通知该项目已更改”?
我可以使用class Animal
def animal_sound
@sound = "roar"
@sound + " "+"animal"
end
# This reader-method lets us see the value of the @eye_colour instance-variable
def eye_colour
"My eyes are: #{@eye_colour}"
end
# This writer-method lets us set the value of the @eye_colour instance variable
def eye_colour=(new_colour)
@eye_colour = new_colour
end
end
class Human < Animal
def human_sound
@sound + " "+"human"
end
# inherits the colour-methods from Animal
end
# when you create a new instance of human, you can give them an eye-colour
human = Human.new
human.eye_colour = "Green"
human.eye_colour # => "My eyes are: Green"
# if you create a new instance of human, you can give them a different eye colour
human2 = Human.new
human2.eye_colour = "Brown"
human2.eye_colour # => "My eyes are: Brown"
# but the first human is still the original colour -
# because the variable contains a value just for *that* instance.
# This is what it means to be an instance-variable
human.eye_colour # => "My eyes are: Green"
吗?如果我使用onActivityResult
从片段中调用第二个Activity,我可以在同一片段中定义startActivityForResult
,然后以某种方式使用Intent数据来查看,“好吧,我需要刷新此位置RecyclerView“?
答案 0 :(得分:2)
我可以使用onActivityResult吗?
是。以下是您可以使用它的方法:
RecyclerView
中点击某个项目时,您会跟踪所点击项目的适配器位置。您从片段中开始第二个结果活动。
// adapterPosition is the position of the clicked item
Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtras("adapter_position", adapterPosition);
startActivityForResult(intent, 1);
在SecondActivity
中,将adapterPosition设置为结果Intent
的一部分。处理SecondActivity
会导致您的片段。
在SecondActivity
// adapterPosition is what you received from your fragment
Intent result = new Intent();
result.putExtra("adapter_position", adapterPosition);
// put other stuff you might need
setResult(Activity.RESULT_OK, result);
finish();
片段
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
int adapterPosition = data.getIntExtra("adapter_position");
// Update your RecyclerAdapter data.
// You will need to define this method in your RecyclerAdapter
yourAdapter.notifyItemChanged(adapterPosition);
}
else {
// Some error/cancel handling
}
}
}
答案 1 :(得分:0)
从片段中调用startActivityForResult
来更新您的项目并发送它的当前位置。
intent.putExtra(POSITION_OF_RECYCLERVIEW,position);
并在DetailActivity
的“保存”按钮上调用setResult()
private void setResult(){
Intent intent = new Intent();
intent.putExtra("UPDATE_POS", position);
intent.putExtra("yourModelClassOrDataType",dataToUpdate);
setResult(RESULT_OK, intent);
finish();
}
答案 2 :(得分:0)
在RecyclerView
中点击某个项目时,通过将该位置放入意图并使用startActivityForResult(intent,1)
开始第二个活动来跟踪所点击项目的适配器位置。
在fragment
:
// adapterPosition is the position of the clicked item
Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtras("adapter_position", adapterPosition);
startActivityForResult(intent, 1);
您将发送adapterPosition以及用户在SecondActivity
返回片段时检索到的任何新数据。您可以通过将其添加到结果Intent
中来发送,如下面的代码所示。
在SecondActivity
:
// adapterPosition is what you received from your fragment
Intent result = new Intent();
result.putExtra("adapter_position", adapterPosition);
result.putExtra("new_item_name", itemName);
// put other stuff you might need
setResult(Activity.RESULT_OK, result);
finish();
现在,在fragment
中,您可以从SecondActivity
检索adapterPosition和新数据。然后使用我在doSomethingWithNewData
下面命名的方法使用新数据操作原始列表。您需要创建此方法。使用新数据更新列表后,在调用我调用updateAdapter
的适配器中的方法时,将此列表传递给适配器。这是您添加到适配器的方法。
回到fragment
:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
if(data != null){
int adapterPosition = data.getIntExtra("adapter_position", -1);
String itemName = data.getStringExtra("new_item_name", -1);
// Do something with the data passed back to fragment
// ie. update the list attached to RecyclerView's adapter
yourList = doSomethingWithNewData(adapterPosition, itemName);
// add this method to your adapter passing new list
yourAdapter.updateAdapter(yourList);
}
}
else {
// Some error/cancel handling
}
}
}
最后,这是你的适配器。您可以在此处将updateAdapter
方法添加到适配器的末尾。在updateAdapter
中,您将传递新的更新列表,并通过notifyDataSetChanged()
通知数据已更改。
在YourAdapter
:
private class YourAdapter extends RecyclerView.Adapter<YourAdapter.YourViewHolder>{
List<Object> yourList;
public YourAdapter(List yourList){
this.yourList = yourList;
}
public class YourViewHolder extends RecyclerView.ViewHolder {
TextView text_1;
TextView text_2;
public NoteViewHolder(View itemView) {
super(itemView);
text_1 = (TextView)itemView.findViewById(R.id.text_1);
text_2 = (TextView)itemView.findViewById(R.id.text_2);
}
}
public YourAdapter.YourViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// inflate a layout to contain a single row from your list
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_row_from_list, parent, false);
NoteViewHolder yVH = new YourViewHolder(view);
return yVH;
}
@Override
public void onBindViewHolder(NotesAdapter.NoteViewHolder holder, int position) {
// set the texts using your modeled list
holder.text_1.setText(yourList.get(position).getTitle());
holder.text_2.setText(yourList.get(position).getMessage());
}
@Override
public int getItemCount() {
// set item count to the size of your list
return yourList.size();
}
// this is the method to add to the adapter
public void updateAdapter(List<Object> yourList){
this.yourList = yourList; // assign data passed in to the adapter list
// notify that the data has changed
notifyDataSetChanged();
}
}