Recycler视图再次创建相同的视图

时间:2016-03-13 09:09:21

标签: android firebase android-recyclerview recycler-adapter

这就是发生的事情...... i have taken screenshot of recyclerview

视图再次创建n。我想要为单个数据创建一次jst视图。

可能是getItemCount()

中的data.size()

适配器

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.MyChatViewHolder> {
private LayoutInflater layoutInflater;
public ClickListener mClickListener;
List<fireInfo> Data= Collections.EMPTY_LIST;
public Context context;
private String EmailPref;



public ChatAdapter(){
    //KEEP it empty !!
}

public ChatAdapter(Context context, List<fireInfo> Data) {
    layoutInflater = LayoutInflater.from(context);

    this.Data=Data;
}

@Override
public MyChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.chat_view_list, parent, false);
    MyChatViewHolder viewHolder = new  MyChatViewHolder(view);
    Log.e("pavan", "hey im called chatADAPTER");
    context = parent.getContext();
    ButterKnife.bind(parent);
    return viewHolder;
}

@Override
public void onBindViewHolder(MyChatViewHolder holder, int position) {
    fireInfo current=Data.get(position);
    Log.e("pavan", "hey im called chatADAPTER" + current.Namee);

     String keylocal = current.KeyCur;
    Log.e("pavan", "Chat Adapter key" + current.KeyCur+position);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    prefs.getString("keylocal"+position,keylocal+position);
    if (keylocal!=null){
        prefs.edit().putString("keylocal"+position,keylocal+position ).commit();
    }

    holder.mName.setText(current.Namee);
    holder.mStatus.setText(current.UserStatus);
    Picasso.with(context).load(current.ImageUrl).transform(new CircleTransform()).into(holder.mImageProfile);

}

public void setClickListener(ClickListener clickListener) {
    this.mClickListener = clickListener;
}
public interface ClickListener{
    public  void itemClicked(View view,int position);

}



@Override
public int getItemCount() {
    Log.e("pavan", "Chat Adapter data sizeee  " +Data.size());
    return Data.size();
}

如何解决这个问题。提前谢谢。

编辑:添加

public class ChatFragment extends Fragment implements ClickListener,SheetLayout.OnFabAnimationEndListener{

// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
private static int i=0;
private static int j;
List<fireInfo> Data= Collections.EMPTY_LIST;
// TODO: Customize parameters
private int mColumnCount = 1;
private ChatAdapter adapter;
private OnListFragmentInteractionListener mListener;
//private static String[] name=new String[40];



public static List<String> name =new LinkedList<>(Collections.<String>emptySet());
public static List<String> image = new LinkedList<>(Collections.<String>emptySet());
public static List<String> status = new LinkedList<>(Collections.<String>emptySet());
public static List<String> key = new LinkedList<>(Collections.<String>emptySet());

public Button mAddbutton;
public FloatingActionButton fab1;
public FloatingActionButton fab2;
public SheetLayout mSheetLayout ;
private static final int REQUEST_CODE = 1;



/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public ChatFragment() {
}

// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static ChatFragment newInstance(int columnCount) {
    ChatFragment fragment = new ChatFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_COLUMN_COUNT, columnCount);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_chat, container, false);

    fab1 = (FloatingActionButton)view. findViewById(R.id.fabchat);
    fab2 = (FloatingActionButton) view.findViewById(R.id.fabchat2);




    Log.e("pavan", "hey Chatfragment called");
    // Set the adapter
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.Chatlist);
        adapter=new ChatAdapter(getActivity(),getDataOther());
        //if(getDataOther()==null){
            Log.e("pavan", "data NULL " + getDataOther());//}
        adapter.setClickListener(this);


        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
    alphaAdapter.setDuration(500);
    alphaAdapter.setInterpolator(new OvershootInterpolator(.5f));
    alphaAdapter.setFirstOnly(false);
    recyclerView.setAdapter(new ScaleInAnimationAdapter(alphaAdapter));

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    prefs.edit().putBoolean("CallAdd", false).commit();


    mSheetLayout=(SheetLayout)view.findViewById(R.id.bottom_sheet);

    mSheetLayout.setFab(fab1);
    mSheetLayout.setFabAnimationEndListener(this);
    fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (fab1.getVisibility() == View.VISIBLE) {
                mSheetLayout.expandFab();
                fab1.setVisibility(View.GONE);
                fab2.setVisibility(View.VISIBLE);


            }
        }
    });

    return view;
}


@Override
public void onFabAnimationEnd() {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.chatFARG, new AddFriendsFragment(), "Login");
    ft.commit();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_CODE){
        mSheetLayout.contractFab();
    }
}




enum Type {
    AlphaIn {
        List<fireInfo> Data= Collections.EMPTY_LIST;
         public AnimationAdapter get(Context context) {

            ChatAdapter adapter = new ChatAdapter(context,  Data);
            return new AlphaInAnimationAdapter(adapter);
        }
    }; public abstract AnimationAdapter get(Context context);
}


public static List<fireInfo> getDataOther(){
    List<fireInfo>data=new ArrayList<>();

    final Firebase mFirebaseRef = new Firebase(constants.FIREBASE_URL+"rootssahaj/authGplus");
    Query queryRef = mFirebaseRef.orderByKey();

    queryRef.addChildEventListener(new ChildEventListener() {
        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Log.e("pavan", "Query FIREBASE in CHAT FRAGMENT" + dataSnapshot.getKey());
            List<fireInfo>data=new ArrayList<>();
            if (!Objects.equals(dataSnapshot.getKey(), "sahaj9917730102@gmail")) {
                key.add(i,dataSnapshot.getKey());
                Log.e("pavan", "Query FIREBASE keyy" + dataSnapshot.getKey());
                Log.e("pavan", "Query FIREBASE" + dataSnapshot.getValue());
                Map<String, String> map = dataSnapshot.getValue(Map.class);



                name.add(i, map.get("userNAME"));
                image.add(i, map.get("picurl"));
                status.add(i, map.get("status"));




                i++;
            }
        }

        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            List<fireInfo>data=new ArrayList<>();
            if (!Objects.equals(dataSnapshot.getKey(), "sahaj9917730102@gmail")) {
                Log.e("pavan", "Query FIREBASE" + dataSnapshot.getValue());
                Map<String, String> map = dataSnapshot.getValue(Map.class);



               // name.set(i, map.get("userNAME"));
              //  image.set(i, map.get("picurl"));
              //  name.set(i, map.get("status"));


            //    i++;
            }
        }

        @TargetApi(Build.VERSION_CODES.KITKAT)
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            List<fireInfo>data=new ArrayList<>();
            if (!Objects.equals(dataSnapshot.getKey(), "sahaj9917730102@gmail")) {
                Log.e("pavan", "Query FIREBASE" + dataSnapshot.getValue());
                Map<String, String> map = dataSnapshot.getValue(Map.class);




                //name.set(i, map.get("userNAME"));
               // image.set(i, map.get("picurl"));
              //  name.set(i, map.get("status"));


              //  i++;
            }
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
    for(int k=1;k<i;k++){
    fireInfo current=new fireInfo();
    current.Namee=name.get(k);
    current.ImageUrl=image.get(k);
    current.UserStatus=status.get(k);
    current.KeyCur=key.get(k);
    data.add(current);}
    return data;
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof OnListFragmentInteractionListener) {
        mListener = (OnListFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnListFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;

}

@Override
public void itemClicked(View view, int position) {

}


public interface OnListFragmentInteractionListener {
    // TODO: Update argument type and name
    void onListFragmentInteraction();
}

}

编辑:解决方案

我得到了纠正,实际上“Lucius Hipan”是正确的,因为传递数据存在问题..数据再次传递给n再次'为什么recyclelerView正在这样做。谢谢!

0 个答案:

没有答案