我有一个带有两个滑动标签(片段)的活动。我想动态地为Recyclerview创建一个标题,这样用户就可以获得一个带有edittext的dialogBox。只要用户在edittext中键入标题名称后单击“确定”,字符串就会从活动传递到片段。我已成功使用接口将标题名称从活动转移到片段。片段中的方法将接收该字符串。
方法:
@Override
public void setDatainfo(String datainfo) {
Log.d("receivedset",datainfo);
AddSection(datainfo);
}
但是,我需要使用收到的字符串为该片段选项卡中的Recyclerview创建一个新标头,因此它将传递给AddSection()方法。我已经在该片段的onCreateView中初始化了必要的类。我只需要在AddSection()方法中执行此操作。
AddSection方法:
public void AddSection(String datainfo){
MyadapterNew myadapterNew = new MyadapterNew(datainfo,getActivity(),R.layout.headerpond,R.layout.file_row_new,fDataset);
Log.d("received",datainfo);
sectionAdapter.addSection(myadapterNew);
sectionAdapter.notifyDataSetChanged();
}
它必须创建一个新标题,但它不是创建一个标题。
FragmentCode:
public class tab1 extends android.support.v4.app.Fragment implements home.infopass {
public ArrayList<String> fDataset = new ArrayList<>();
private Context context;
private SectionedRecyclerViewAdapter sectionAdapter;
RecyclerView fRecyclerView;
@Override
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab1, container, false);
Log.d("tab1", "OncreateView");
registerForContextMenu(v);
sectionAdapter = new SectionedRecyclerViewAdapter();
MyadapterNew myadapterNew = new MyadapterNew("Pond1",getActivity(),R.layout.headerpond,R.layout.file_row_new,fDataset);
sectionAdapter.addSection(myadapterNew);
fRecyclerView = (RecyclerView) v.findViewById(R.id.file_tab);
fRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager fLayoutManager = new LinearLayoutManager(this.getContext());
fRecyclerView.setLayoutManager(fLayoutManager);
fRecyclerView.setAdapter(sectionAdapter);
return v;
}
@Override
public void setDatainfo(String datainfo) {
Log.d("receivedset",datainfo);
AddSection(datainfo);
}
public void AddSection(String datainfo){
MyadapterNew myadapterNew = new MyadapterNew(datainfo,getActivity(),R.layout.headerpond,R.layout.file_row_new,fDataset);
Log.d("received",datainfo);
sectionAdapter.addSection(myadapterNew);
sectionAdapter.notifyDataSetChanged();
}
private class MyadapterNew extends StatelessSection {
List<String> data = new ArrayList<>(Arrays.asList("xyz", "abc", "xyhgz", "abygc"));
String header;
Context ctx;
Boolean Expanded = false;
public MyadapterNew(String header,Context ctx,int headerResourceId, int itemResourceId,ArrayList<String>data) {
super(headerResourceId, itemResourceId);
//this.data = data;
this.ctx=ctx;
this.header=header;
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView header;
public HeaderViewHolder(View view) {
super(view);
header = (TextView)view.findViewById(R.id.header);
}
}
class MyItemViewHolder extends RecyclerView.ViewHolder {
private TextView filename;
private ProgressBar file_progressbar;
private ImageView fileicon;
private ImageView upldownstatus;
private TextView filelength;
public MyItemViewHolder(View itemView) {
super(itemView);
filename = (TextView) itemView.findViewById(R.id.filename);
file_progressbar = (ProgressBar) itemView.findViewById(R.id.file_progressbar);
fileicon = (ImageView)itemView.findViewById(R.id.fileicon);
upldownstatus = (ImageView)itemView.findViewById(R.id.updownstatus);
filelength = (TextView)itemView.findViewById(R.id.filelength);
}
}
public void addItem(int position, String item) {
}
public void removeItem(int position) {
this.data.remove(position);
sectionAdapter.notifyDataSetChanged();
}
public void addHeader(int position, String item) {
}
public void removeHeader(int position) {
}
@Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new MyItemViewHolder(view);
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
final MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
itemHolder.filename.setText(data.get(position));
final Animation animationFadeOut = AnimationUtils.loadAnimation(ctx, R.anim.fadeout);
itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("CLICKED", "TRUE");
itemHolder.itemView.setAnimation(animationFadeOut);
}
});
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
super.onBindHeaderViewHolder(holder);
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
((HeaderViewHolder) holder).header.setText(header);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expanded=!Expanded;
sectionAdapter.notifyDataSetChanged();
}
});
}
@Override
public int getContentItemsTotal() {
return Expanded? data.size():0;
}
}
}
ERROR:
05-01 01:26:08.915 1571-1571/com.example.sandesh.filer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sandesh.filer, PID: 1571
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter.addSection(io.github.luizgrp.sectionedrecyclerviewadapter.Section)' on a null object reference
at com.example.sandesh.filer.tab1.AddSection(tab1.java:197)
at com.example.sandesh.filer.tab1.setDatainfo(tab1.java:90)
at com.example.sandesh.filer.home$3.onClick(home.java:313)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
答案 0 :(得分:0)
你的AddSection方法似乎是正确的,你确定在调用AddSection后没有触发onCreateView方法吗?请检查你的日志。
答案 1 :(得分:0)
我解决了我的问题。我怀疑,一旦用户输入标题名称,标题就会传入片段。但是,字符串是无用的,因为sectionAdapter实例不再存在以使用该字符串并创建标头。我必须得到片段的实例,它已经在开始时实例化(当应用程序启动时)。所以这就是我做的工作。
int position = tabLayout.getSelectedTabPosition
Fragment fragment = adapter.getFragment(tabLayout
.getSelectedTabPosition());
if (fragment != null) {
switch (position) {
case 0:
((tab1) fragment).AddSection(data);
break;
}
}
我已经使用上面的代码将头字符串发送到片段。当我使用pager作为我的TabLayout时,我已经实现了getfragment方法来获取我的pagerAdapter中已经实例化的片段的实例。
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
final Fragment fragment = (Fragment) super.instantiateItem(container, position);
instantiatedFragments.put(position, new WeakReference<>(fragment));
return fragment;
}
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
instantiatedFragments.remove(position);
super.destroyItem(container, position, object);
}
@Nullable
public Fragment getFragment(final int position) {
final WeakReference<Fragment> wr = instantiatedFragments.get(position);
if (wr != null) {
return wr.get();
} else {
return null;
}
}
有关详细信息,请查看此Link,希望它对某人有用