2015-11-20 3 views
0

У меня есть любимый флажок в моем recyclerview. Моя проблема заключается в том, что флажок не включен в recyclerview. и я должен передать отмеченные элементы рециркуляции в другой recyclerview. На самом деле я застрял здесь. кто-нибудь, пожалуйста, помогите мне. Заранее спасибо.CheckBox в recyclerview не работает и не может передать отмеченные элементы другому фрагменту

recycler_view_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="210dp" 
    android:background="@color/cardview_light_background" 
    android:orientation="vertical"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="210dp"> 
    <View 
     android:id="@+id/divider" 
     android:layout_width="match_parent" 
     android:layout_height="2dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:background="@color/common_action_bar_splitter" /> 

    <ImageView 
     android:id="@+id/PROJECT_image" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/list_item_avatar_size" 
     android:layout_below="@id/divider" 
     android:background="@drawable/mirlogo" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" STATUS " 
     android:background="@color/common_action_bar_splitter" 
     android:id="@+id/PROJECT_status" 
     android:rotation="-45" 
     android:layout_below="@+id/divider" 
     android:layout_alignLeft="@+id/divider" 
     android:layout_alignStart="@+id/divider" 
     android:layout_marginTop="15dp" /> 
</RelativeLayout> 
    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#80000000" 
     android:layout_alignParentBottom="true"> 

     <TextView 
      android:id="@+id/PROJECT_name" 
      android:textColor="@color/cardview_light_background" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Project" 
      android:textSize="18sp" 
      android:textAppearance="?attr/textAppearanceListItem" /> 

     <TextView 
      android:id="@+id/PROJECT_city" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PROJECT_name" 
      android:text="kerala" 
      android:textColor="@color/common_action_bar_splitter" 
      android:textSize="16sp" 
      android:textAppearance="?attr/textAppearanceListItem" /> 

     <TextView 
      android:id="@+id/PROJECT_type" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/PROJECT_city" 
      android:text="india" 
      android:background="@android:color/holo_green_dark" 
      android:textColor="@color/cardview_light_background" 
      android:textSize="15sp" 
      android:textAppearance="?attr/textAppearanceListItem" /> 

     <CheckBox 
      android:id="@+id/PROJECT_fav" 
      android:layout_width="30sp" 
      android:layout_height="30sp" 
      android:layout_alignBottom="@+id/PROJECT_city" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="5dp" 
      android:layout_marginEnd="5dp" 
      android:background="@drawable/selector" 
      android:button="@null" 
      /> 

    </RelativeLayout> 

</RelativeLayout> 

RecyclerviewAdapter

import android.support.v7.widget.RecyclerView; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckBox; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.example.root5solutions.mirrealtors.R; 

public class HomeDataManager extends RecyclerView.Adapter<HomeDataManager.RecyclerViewHolder> { 

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder { 

     TextView mProjectName, mProjectCity, mProjectType, mProjectStatus; 
     ImageView mImage; 
     CheckBox mCheck; 


     RecyclerViewHolder(View itemView) { 
      super(itemView); 
      mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name); 
      mProjectCity = (TextView) itemView.findViewById(R.id.PROJECT_city); 
      mProjectType = (TextView) itemView.findViewById(R.id.PROJECT_type); 
      mProjectStatus = (TextView) itemView.findViewById(R.id.PROJECT_status); 
      mImage = (ImageView) itemView.findViewById(R.id.PROJECT_image); 
      mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav); 
     } 
    } 

    @Override 
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false); 
     return new RecyclerViewHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) { 
     // get the single element from the main array 

     final HomeProjects projects = HomeProjects.PROJECTS[i]; 
     // Set the values 
     viewHolder.mProjectName.setText(projects.get(HomeProjects.Field.NAME)); 
     viewHolder.mProjectCity.setText(projects.get(HomeProjects.Field.CITY)); 
     viewHolder.mProjectType.setText(projects.get(HomeProjects.Field.TYPE)); 
     viewHolder.mProjectStatus.setText(projects.get(HomeProjects.Field.STATUS)); 
     viewHolder.mImage.setImageResource(projects.geti(HomeProjects.Field.IMAGE)); 
     viewHolder.mCheck.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       viewHolder.mCheck.setChecked(!viewHolder.mCheck.isChecked()); 
       Log.d("Tag Name", "Log Message"); 

      } 
     }); 

    @Override 
    public int getItemCount() { 

     return HomeProjects.PROJECTS.length; 
    } 
} 

RecyclerviewFragment

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.root5solutions.mirrealtors.HomeRecyclerDetailActivity; 
import com.example.root5solutions.mirrealtors.ProjectRecyclerDetailActivity; 
import com.example.root5solutions.mirrealtors.R; 
import com.example.root5solutions.mirrealtors.projectdatabase.HomeDataManager; 
import com.example.root5solutions.mirrealtors.projectdatabase.HomeProjects; 
import com.example.root5solutions.mirrealtors.projectdatabase.Projects; 
import com.example.root5solutions.mirrealtors.projectdatabase.RecyclerClickListener; 
import com.kogitune.activity_transition.ActivityTransitionLauncher; 

public class RecyclerTab1 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.home_tab1_recycler, container, false); 
     RecyclerView rv = (RecyclerView) v.findViewById(R.id.home_recyclerview); 
     LinearLayoutManager llm = new LinearLayoutManager(getContext()); 
     rv.setLayoutManager(llm); 
     rv.setHasFixedSize(true); // to improve performance 
     rv.setAdapter(new HomeDataManager()); // the projectdatabase manager is assigner to the RV 
     rv.addOnItemTouchListener(// and the click is handled 
       new RecyclerClickListener(getContext(), new RecyclerClickListener.OnItemClickListener() { 
        @Override 
        public void onItemClick(View view, int position) { 
         Intent intent = new Intent(getActivity(), HomeRecyclerDetailActivity.class); 
         intent.putExtra(HomeRecyclerDetailActivity.ID, HomeProjects.PROJECTS[position].getId()); 
         ActivityTransitionLauncher.with(getActivity()).from(view).launch(intent); 
        } 
       })); 
     return v; 
    } 
} 
+0

@Mike M: Sir. У меня есть настраиваемая форма для проверенного и не проверенного состояния. Когда я удаляю android: button = "@ null", флажок показывает квадрат квадрата по умолчанию. –

+0

К сожалению, я не заметил атрибут 'background'. Вместо этого вы хотите сделать свой атрибут 'button'. I.e., 'android: button =" @ drawable/selector "'. –

+0

@RubinNellikunnathu У меня такая же проблема, но не в состоянии решить –

ответ

0

Снимите onClickListener и установите onCheckedChangeListener. Затем сохраните свой список в массиве в Sharedpreference.

viewHolder.mCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if (isChecked) { 
      Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item Favorited", Snackbar.LENGTH_SHORT); 
      snackbar.show(); 
     } else { 
      Snackbar snackbar = Snackbar.make(viewHolder.itemView, "Item Unfavorited", Snackbar.LENGTH_SHORT); 
      snackbar.show(); 
     } 
     int position = viewHolder.getAdapterPosition(); 
     mCheckedItems.put(position, isChecked); 
     List<Integer> selected = new ArrayList<>(); 
     for (int i = 0; i < mCheckedItems.size(); i++) { 
      final boolean checked = mCheckedItems.valueAt(i); 
      if (checked) { 
       selected.add(mCheckedItems.keyAt(i)); 
      } 
     } 
     Log.e("Checked Array = ", String.valueOf(selected)); 
     SharedPreferences prefs = v1.getContext().getSharedPreferences("my_prefs", Context.MODE_PRIVATE); 
     SharedPreferences.Editor edit = prefs.edit(); 
     edit.putString("Checkbox", String.valueOf(selected)); 
     edit.commit(); 
     Log.e("Shared Preference = ", String.valueOf(edit)); 
    } 
});