我正在做recyclerview我有一个活动,我想在那个有recyclerview的活动中显示我的片段。我不知道我哪里出错了请帮我这个
这是我的活动 CategoryHairActivity.java import com.saloon.fragments.HairCategoryFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class CategoryHairActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category_hair);
HairCategoryFragment hairCategoryFragment=new HairCategoryFragment();
if(hairCategoryFragment!=null){
FragmentManager fmanager=getSupportFragmentManager();
FragmentTransaction ft=fmanager.beginTransaction();
ft.add(R.id.mainContent, hairCategoryFragment);
ft.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category_hair, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的片段
HairCategoryFragment.java
import java.util.ArrayList;
import java.util.List;
import com.saloon.adapters.HairCategoryAdapter;
import com.saloon.callbacks.HairCategoryInformation;
import com.saloon.myapp.R;
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;
public class HairCategoryFragment extends Fragment {
public HairCategoryAdapter adapter;
private RecyclerView recyclerView;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.hair_service_list_view, container, false);
adapter=new HairCategoryAdapter(getActivity(), getData());
recyclerView=(RecyclerView) rootView.findViewById(R.id.serviceListView);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return rootView;
}
private List<HairCategoryInformation> getData() {
List<HairCategoryInformation> data = new ArrayList<HairCategoryInformation>();
String[] serviceNames ={"razor cut","lime cut","onion cut","step cut"};
String[] servicePrices={"200","300","400","500"};
for(int i=0;i<serviceNames.length && i<servicePrices.length;i++){
HairCategoryInformation current = new HairCategoryInformation();
current.serviceName=serviceNames[i];
current.servicePrice=servicePrices[i];
data.add(current);
}
return data;
}
}
Viewholder.java
import com.saloon.myapp.R;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class HairCategoryViewHolder extends ViewHolder{
public CheckBox checkbox;
public TextView name,price;
public HairCategoryViewHolder(View itemView) {
super(itemView);
checkbox=(CheckBox) itemView.findViewById(R.id.serviceSelect);
name=(TextView) itemView.findViewById(R.id.serviceName);
price=(TextView) itemView.findViewById(R.id.servicePrice);
}
}
HairCategoryInformation.java
public class HairCategoryInformation {
public String serviceName,servicePrice;
}
HairCategoryAdapter.java
import java.util.Collections;
import java.util.List;
import com.saloon.callbacks.HairCategoryInformation;
import com.saloon.viewholder.HairCategoryViewHolder;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;
public class HairCategoryAdapter extends RecyclerView.Adapter<HairCategoryViewHolder>{
private LayoutInflater inflater;
private List<HairCategoryInformation> hairCategoryInformations = Collections.emptyList();
private Context context;
String text;
public HairCategoryAdapter(Context context,List<HairCategoryInformation> hairCategoryInformations) {
this.context=context;
inflater=LayoutInflater.from(context);
this.hairCategoryInformations=hairCategoryInformations;
}
@Override
public HairCategoryViewHolder onCreateViewHolder(ViewGroup parent, int type) {
View view=inflater.inflate(com.saloon.myapp.R.layout.hair_category_list, parent,false);
HairCategoryViewHolder holder=new HairCategoryViewHolder(view);
return holder;
}
@Override
public int getItemCount() {
return hairCategoryInformations.size();
}
@Override
public void onBindViewHolder(HairCategoryViewHolder holder, int position) {
HairCategoryInformation current=hairCategoryInformations.get(position);
holder.name.setText(current.serviceName);
holder.price.setText(current.servicePrice);
holder.checkbox.setTag(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, v.getTag().toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
答案 0 :(得分:0)
为什么在初始化后检查hairCategoryFragment为null?无论如何,代码似乎很好,似乎你在布局上有问题。