ViewHolder在Fragment中不起作用

时间:2015-11-24 13:39:53

标签: android android-fragments android-recyclerview android-viewholder

我在片段中使用RecyclerView但是当我运行应用程序时,在屏幕上不显示recyclerView。 调用CustomAdapter构造函数但未调用ViewHolder方法。

请帮忙。

以下是片段代码。

public class StatFragment extends Fragment {

List<UsageLog> usageLogs;
List<UsageLog> loadWiseUsageLogs;
int recordType;
StatsAdapter statsAdapter;
RecyclerView statRecyclerView;

public StatFragment() {
    // Required empty public constructor
}


public static StatFragment newInstance(int recordType) {

    Bundle args = new Bundle();
    args.putInt("recordType", recordType);
    StatFragment fragment = new StatFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    recordType=getArguments().getInt("recordType");
    loadWiseUsageLogs=UsageLog.getOverview(new Date(Calendar.getInstance().getTimeInMillis()), recordType);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v= inflater.inflate(R.layout.fragment_stat, container, false);
    statRecyclerView =(RecyclerView)v.findViewById(R.id.stat_recycler_view);

    statsAdapter=new StatsAdapter(loadWiseUsageLogs);
    statRecyclerView.setAdapter(statsAdapter);
    statRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

/***************************************************************************************************
 //Code for RecylerView Adapter and View Holder
 ***************************************************************************************************/

class StatsAdapter extends RecyclerView.Adapter<StatsAdapter.ViewHolder>{
    List<UsageLog> usageLogs;

    public StatsAdapter(List<UsageLog> usageLogs) {
        this.usageLogs = usageLogs;
        Log.d("Adapter","called"+"size="+usageLogs.size());
    }

    @Override
    public StatsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {            
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View loadStatView=inflater.inflate(R.layout.element_stat_load_single, parent, false);
        ViewHolder loadViewHolder=new ViewHolder(loadStatView);
        Log.d("onCreateVH","called");
        return loadViewHolder;
    }

    @Override
    public void onBindViewHolder(StatsAdapter.ViewHolder holder, int position) {            holder.tvLoadName.setText(usageLogs.get(position).getLoad().getName());
        holder.tvLoadUnits.setText(usageLogs.get(position).getUnits());
        Log.d("onBind", "called");
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    class ViewHolder  extends RecyclerView.ViewHolder{
        TextView tvLoadName,tvLoadUnits;
        ImageView imageLoad;

        public ViewHolder(View itemView) {
            super(itemView);
            tvLoadName=(TextView) itemView.findViewById(R.id.stat_load_name);
            tvLoadUnits=(TextView) itemView.findViewById(R.id.stat_load_unit);
            imageLoad=(ImageView)itemView.findViewById(R.id.stat_load_icon);
        }
    }
}

}

0 个答案:

没有答案