2016-03-08 63 views
0

我有有一定的价值,现在我想添加2个部分之一favourites另一个用于defaults一个RecyclerView我可以手动像这样做:上recyclerView添加部分动态运行时(爪哇)

我有ChannelsAdapter为保持的值:

public class ChannelsAdapter extends RecyclerView.Adapter<ChannelsAdapter.ChannelsViewHolder> implements Filterable { 

private LayoutInflater inflater; 
private Context context; 

List<ChannelsInformation> data = Collections.emptyList(); 

private final List<ChannelsInformation> filteredChannelsList; 

private final MultiSelector mMultiSelector = new MultiSelector(); 

    ArrayList <String> selectedChannelName , selectedChannelID; 

private HashMap<String, Boolean> map; 


private TabFragment5 tabFragment5; 


public ChannelsAdapter(Context context, List<ChannelsInformation> data){ 

    inflater = LayoutInflater.from(context); 


    this.context = context; 


    this.data = data; 


    filteredChannelsList = data; 
} 

public void remove(int position){ 
    data.remove(position); 
    notifyItemRemoved(position); 
} 


@Override 
public ChannelsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    View rowView = inflater.inflate(R.layout.custom_channel_row, parent, false); 

    ChannelsViewHolder holder = new ChannelsViewHolder(rowView); 

    return holder; 
} 

@Override 
public void onBindViewHolder(final ChannelsViewHolder holder, final int position) { 

    final ChannelsInformation current = data.get(position); 

    holder.CHANNELNAME.setText(current.channelName); 


    selectedChannelName = new ArrayList<String>(); 
    selectedChannelID = new ArrayList<String>(); 

    holder.mSolvedCheckBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (!selectedChannelID.contains(current.id)) { 
       holder.mSolvedCheckBox.setChecked(true); 
       selectedChannelName.add(current.channelName); 
       selectedChannelID.add(current.id); 

      } else { 
       holder.mSolvedCheckBox.setChecked(false); 

       selectedChannelName.remove(current.channelName); 
       selectedChannelID.remove(current.id); 

      } 

     } 
    }); 

} 

@Override 
public int getItemCount() { 
    return data.size(); 
} 

@Override 
public Filter getFilter() { 
    return new UserFilter(this ,filteredChannelsList); 
} 



private static class UserFilter extends Filter { 

    private final ChannelsAdapter adapter; 

    private final List<ChannelsInformation> originalList; 

    private final List<ChannelsInformation> filteredList; 

    private UserFilter(ChannelsAdapter adapter, List<ChannelsInformation> originalList) { 
     super(); 
     this.adapter = adapter; 
     this.originalList = new ArrayList<>(originalList); 
     this.filteredList = new ArrayList<>(); 
    } 

    @Override 
    protected FilterResults performFiltering(CharSequence constraint) { 
     filteredList.clear(); 
     final FilterResults results = new FilterResults(); 

     if (constraint == null || constraint.length() == 0) { 
      filteredList.addAll(originalList); 



     } else { 
      final String filterPattern = constraint.toString().toLowerCase().trim(); 


      for (final ChannelsInformation channel : originalList) { 

       if ((channel.channelName != null && channel.channelName.toLowerCase().contains(filterPattern)) 
         ) 

       { 
        filteredList.add(channel); 

       } 

      } 
     } 
     results.values = filteredList; 
     results.count = filteredList.size(); 
     return results; 
    } 


    @Override 
    protected void publishResults(CharSequence constraint, FilterResults results) { 
     adapter.filteredChannelsList.clear(); 
     if ((ArrayList<ChannelsInformation>) results.values != null) { 

      adapter.filteredChannelsList.addAll((ArrayList<ChannelsInformation>) results.values); 
     } 
     adapter.notifyDataSetChanged(); 

    } } 






class ChannelsViewHolder extends SwappingHolder implements View.OnClickListener { 

    TextView CHANNELNAME; 
    CheckBox mSolvedCheckBox; 




    public ChannelsViewHolder(View itemView) { 


     super(itemView , mMultiSelector); 

     mMultiSelector.setSelectable(true); 

     mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.selectedChannelCheckBox); 

     itemView.setOnClickListener(this); 

     CHANNELNAME = (TextView) itemView.findViewById(R.id.ChannelNameTxtView); 



    } 

    @Override 
    public void onClick(View v) { 


      Toast.makeText(context, ""+CHANNELNAME.getText() ,Toast.LENGTH_SHORT).show(); 

     tabFragment5 = new TabFragment5(); 

     tabFragment5.addFavoriteSectionToRecyclerView(); // here am calling this function which is throwing me error 

    } 

}} 

和我有一个用于部分SimpleSectionedRecyclerViewAdapter另一个适配器:

public class SimpleSectionedRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

private final Context mContext; 
private static final int SECTION_TYPE = 0; 

private boolean mValid = true; 
private int mSectionResourceId; 
private int mTextResourceId; 
private LayoutInflater mLayoutInflater; 
private RecyclerView.Adapter mBaseAdapter; 
private SparseArray<Section> mSections = new SparseArray<Section>(); 


public SimpleSectionedRecyclerViewAdapter(Context context, int sectionResourceId, int textResourceId, 
              RecyclerView.Adapter baseAdapter) { 

    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mSectionResourceId = sectionResourceId; 
    mTextResourceId = textResourceId; 
    mBaseAdapter = baseAdapter; 
    mContext = context; 

    mBaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 
     @Override 
     public void onChanged() { 
      mValid = mBaseAdapter.getItemCount()>0; 
      notifyDataSetChanged(); 
     } 

     @Override 
     public void onItemRangeChanged(int positionStart, int itemCount) { 
      mValid = mBaseAdapter.getItemCount()>0; 
      notifyItemRangeChanged(positionStart, itemCount); 
     } 

     @Override 
     public void onItemRangeInserted(int positionStart, int itemCount) { 
      mValid = mBaseAdapter.getItemCount()>0; 
      notifyItemRangeInserted(positionStart, itemCount); 
     } 

     @Override 
     public void onItemRangeRemoved(int positionStart, int itemCount) { 
      mValid = mBaseAdapter.getItemCount()>0; 
      notifyItemRangeRemoved(positionStart, itemCount); 
     } 
    }); 
} 


public static class SectionViewHolder extends RecyclerView.ViewHolder { 

    public TextView title; 

    public SectionViewHolder(View view,int mTextResourceid) { 
     super(view); 
     title = (TextView) view.findViewById(mTextResourceid); 
    } 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int typeView) { 
    if (typeView == SECTION_TYPE) { 
     final View view = LayoutInflater.from(mContext).inflate(mSectionResourceId, parent, false); 

     return new SectionViewHolder(view,mTextResourceId); 
    }else{ 

     return mBaseAdapter.onCreateViewHolder(parent, typeView -1); 
    } 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder sectionViewHolder, int position) { 
    if (isSectionHeaderPosition(position)) { 
     ((SectionViewHolder)sectionViewHolder).title.setText(mSections.get(position).title); 

    }else{ 
     mBaseAdapter.onBindViewHolder(sectionViewHolder,sectionedPositionToPosition(position)); 


    } 

} 

@Override 
public int getItemViewType(int position) { 
    return isSectionHeaderPosition(position) 
      ? SECTION_TYPE 
      : mBaseAdapter.getItemViewType(sectionedPositionToPosition(position)) +1 ; 
} 


public static class Section { 
    int firstPosition; 
    int sectionedPosition; 
    CharSequence title; 

    public Section(int firstPosition, CharSequence title) { 
     this.firstPosition = firstPosition; 
     this.title = title; 
    } 

    public CharSequence getTitle() { 
     return title; 
    } 
} 


public void setSections(Section[] sections) { 
    mSections.clear(); 

    Arrays.sort(sections, new Comparator<Section>() { 
     @Override 
     public int compare(Section o, Section o1) { 
      return (o.firstPosition == o1.firstPosition) 
        ? 0 
        : ((o.firstPosition < o1.firstPosition) ? -1 : 1); 
     } 
    }); 

    int offset = 0; // offset positions for the headers we're adding 
    for (Section section : sections) { 
     section.sectionedPosition = section.firstPosition + offset; 
     mSections.append(section.sectionedPosition, section); 
     ++offset; 
    } 

    notifyDataSetChanged(); 
} 

public int positionToSectionedPosition(int position) { 
    int offset = 0; 
    for (int i = 0; i < mSections.size(); i++) { 
     if (mSections.valueAt(i).firstPosition > position) { 
      break; 
     } 
     ++offset; 
    } 
    return position + offset; 
} 

public int sectionedPositionToPosition(int sectionedPosition) { 
    if (isSectionHeaderPosition(sectionedPosition)) { 
     return RecyclerView.NO_POSITION; 
    } 

    int offset = 0; 
    for (int i = 0; i < mSections.size(); i++) { 
     if (mSections.valueAt(i).sectionedPosition > sectionedPosition) { 
      break; 
     } 
     --offset; 
    } 
    return sectionedPosition + offset; 
} 

public boolean isSectionHeaderPosition(int position) { 
    return mSections.get(position) != null; 
} 


@Override 
public long getItemId(int position) { 
    return isSectionHeaderPosition(position) 
      ? Integer.MAX_VALUE - mSections.indexOfKey(position) 
      : mBaseAdapter.getItemId(sectionedPositionToPosition(position)); 
} 

@Override 
public int getItemCount() { 
    return (mValid ? mBaseAdapter.getItemCount() + mSections.size() : 0); 
} 

} 

,这里是我的片断我在哪里增加值和部分TabFragment5这样的:

 channelsAdapter = new ChannelsAdapter(getActivity(), getData()); // getting values 

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    List<SimpleSectionedRecyclerViewAdapter.Section> sections = 
      new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>(); 

    //Sections , first section by default 

    sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "All Channels")); 

    //Add your adapter to the sectionAdapter 
    SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]; 
    SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new 
      SimpleSectionedRecyclerViewAdapter(getActivity(),R.layout.section,R.id.section_text,channelsAdapter); 
    mSectionedAdapter.setSections(sections.toArray(dummy)); 

    recyclerView.setAdapter(mSectionedAdapter); 

//以及上述工作正常,但是这是我所希望实现的50%,我想补充的一个default部分当用户没有设置任何值的收藏和一旦用户做出任何价值最喜欢我想添加第二部分Favourites我不知道如何在运行时动态地做到这一点,我试过这个课程没有工作我得到了一个错误:

public void addFavoriteSectionToRecyclerView(){ 


    Toast.makeText(context, "Function Called" ,Toast.LENGTH_SHORT).show(); 

    channelsAdapter = new ChannelsAdapter(getActivity(), getData()); 

    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 


    List<SimpleSectionedRecyclerViewAdapter.Section> sections = 
      new ArrayList<SimpleSectionedRecyclerViewAdapter.Section>(); 

    //Sections 
    sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "Favorites")); // adding second Section dynamically 
    sections.add(new SimpleSectionedRecyclerViewAdapter.Section(0, "All Channels")); 

    //Add your adapter to the sectionAdapter 
    SimpleSectionedRecyclerViewAdapter.Section[] dummy = new SimpleSectionedRecyclerViewAdapter.Section[sections.size()]; 
    SimpleSectionedRecyclerViewAdapter mSectionedAdapter = new 
    SimpleSectionedRecyclerViewAdapter(getActivity(),R.layout.section,R.id.section_text,channelsAdapter); 
    mSectionedAdapter.setSections(sections.toArray(dummy)); 

    recyclerView.setAdapter(mSectionedAdapter); 

} 

我的错误:

03-08 13:50:41.397 4237-4237/? E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: pb.myPackage, PID: 4237 
    java.lang.NullPointerException 
    at android.view.LayoutInflater.from(LayoutInflater.java:211) 
    at pb.myPackage.ChannelsAdapter.<init>(ChannelsAdapter.java:52) 
    at pb.myPackage.TabFragment5.addFavoriteSectionToRecyclerView(TabFragment5.java:420) 
    at pb.myPackage.ChannelsAdapter$ChannelsViewHolder.onClick(ChannelsAdapter.java:236) 
    at android.view.View.performClick(View.java:4469) 
    at android.view.View$PerformClick.run(View.java:18468) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5021) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
    at dalvik.system.NativeStart.main(Native Method) 

任何想法家伙为什么会出现此错误?如果是的话请指出问题,或者如果我在运行时添加本节的方法不够好,那么请给我一个更好的方法,任何帮助或指导将非常感谢和帮助我,谢谢

P.S.添加这一部分我跟着这个SimpleSectionedRecyclerViewAdapter

UPDATE:

请参阅该图像获得更好的理解,我有一个正常的RecyclerView这里我的推杆命名为defaultsection。现在默认那里,如果用户选择的最喜欢的我想添加名为Favourites另一部分和移动选定值favourites节的任何值是其被命名为default现在只有第一部分

i want to add values into another section like this

+2

你可以在绘画中创建一个图像并发布你想要的东西 - 图形视图。你的q太长,我不明白 – ik024

+0

嘿@ ik024感谢您的答复男人,是的让我更新我的问题 –

+0

嘿@ ik024请检查我更新的问题 –

回答

2
tabFragment5 = new TabFragment5(); 

tabFragment5.addFavoriteSectionToRecyclerView(); // here am calling this function which is throwing me error 

如果TabFragment5确实是一个片段,那么上面的代码将创建一个全新的TabFragment5,然后调用addFavoriteSectionToRecyclerView()

在行之间读取,我假设你想在当前的TabFragment5实例上调用这个方法,并且你不想创建一个新的。

对此,我可以推荐的最简单方法是使用EventBus

根据EventBus文档创建“Event”类,即:

public class AddFavoriteSectionEvent { 

    public AddFavoriteSectionEvent(); 

} 

注册您的片段侦听事件:

@Override 
public void onStart() { 
    super.onStart(); 
    EventBus.getDefault().register(this); 
} 

@Override 
public void onStop() { 
    EventBus.getDefault().unregister(this); 
    super.onStop(); 
} 

添加一个方法到你的片段响应这些事件:

@Subscribe 
public void onAddFavoriteEvent(AddFavoriteSectionEvent event){ 
    addFavoriteSectionToRecyclerView(); 
} 

而更换两行导致您的NPE与:

EventBus.getDefault().post(new AddFavoriteSectionEvent()); 

这应该至少解决您的NPE。

+0

嘿男人非常感谢您的回答,我真的很感谢您的努力,我只是按照您的回答,但在切换到我的片段错误时出现错误:'rg.greenrobot.eventbus。 EventBusException:订阅服务器类pb.myPackage.TabFragment5及其超类没有公共方法,其@Subscribe注释 位于org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67) ,位于org.greenrobot.eventbus.EventBus。注册(EventBus.java:136)'你能告诉我我做错了什么吗? –

+1

@remyboys这必须是一个新东西 - 尝试在'onAddFavoriteEvent()'方法上面添加'@ Subscribe'。 – PPartisan

+0

嘿男人现在感谢很多它的工作正常:),感谢你 –