在recyclerviewadapter上以编程方式更改标题布局

时间:2016-09-14 10:45:03

标签: android header textview android-recyclerview

我带有导航抽屉 我想更改标题布局中的 texview

这里是我的recyclerviewadapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
String[] titles;
TypedArray icons;
Context context;
private TextView saldo;

  RecyclerViewAdapter(String[] titles , TypedArray icons , Context context){

        this.titles = titles;
        this.icons = icons;
        this.context = context;
}



public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    TextView navTitle;
    ImageView navIcon;
    Context context;

    public ViewHolder(View drawerItem , int itemType , Context context){

        super(drawerItem);
        this.context = context;
        drawerItem.setOnClickListener(this);
        if(itemType==1){
            navTitle = (TextView) itemView.findViewById(R.id.tv_NavTitle);
            navIcon = (ImageView) itemView.findViewById(R.id.iv_NavIcon);
        }
    }


    @Override
    public void onClick(View v) {

        MainActivity mainActivity = (MainActivity)context;
        mainActivity.drawerLayout.closeDrawers();
        FragmentTransaction fragmentTransaction = mainActivity.getSupportFragmentManager().beginTransaction();

        switch (getPosition()){
            case 1:
                Fragment squadFragment = new SquadFragment();
                fragmentTransaction.replace(R.id.containerView,squadFragment);
                fragmentTransaction.commit();
                break;
            case 2:
                Fragment fixtureFragment = new FixtureFragment();
                fragmentTransaction.replace(R.id.containerView,fixtureFragment);
                fragmentTransaction.commit();
                break;
            case 3:
                Fragment tableFragment = new TableFragment();
                fragmentTransaction.replace(R.id.containerView,tableFragment);
                fragmentTransaction.commit();
                break;
            case 4:
                Fragment bantuan = new bantuan();
                fragmentTransaction.replace(R.id.containerView,bantuan);
                fragmentTransaction.commit();
                break;
            case 5:
                Fragment tentang = new tentang();
                fragmentTransaction.replace(R.id.containerView,tentang);
                fragmentTransaction.commit();
                break;
            case 6:
                Fragment keluar = new keluar();
                fragmentTransaction.replace(R.id.containerView, keluar);
                fragmentTransaction.commit();
                break;

        }
    }
}


@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(viewType==1){
             View itemLayout =   layoutInflater.inflate(R.layout.drawer_item_layout,null);



             return new ViewHolder(itemLayout,viewType,context);
        }
        else if (viewType==0) {
            View itemHeader = layoutInflater.inflate(R.layout.header_layout,null);
            saldo.findViewById(R.id.saldo);

            saldo.setText("Android");
            return new ViewHolder(itemHeader,viewType,context);
        }






    return null;
}


@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {

    if(position!=0){
        holder.navTitle.setText(titles[position - 1]);
        holder.navIcon.setImageResource(icons.getResourceId(position-1,-1));
    }

}


@Override
public int getItemCount() {
    return titles.length+1;
}


@Override
public int getItemViewType(int position) {
    if(position==0)return 0;
    else return 1;
}



   }

和我的标题布局

<RelativeLayout

<TextView
    android:layout_width="match_parent"
    android:id="@+id/barca"
    android:layout_height="100dp"
    android:text=" Halo,\n\  Kios Untung Selalu"
    android:textColor="#FFFFFF"
    android:background="#3F51B5"
    android:gravity="center_vertical"
    android:textSize="20dp"

    />

<TextView
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:text="Sisa Saldo"
    android:textColor="#3F51B5"
    android:layout_below="@id/barca"
    android:textSize="20dp"
    android:gravity="center_vertical|center"
    android:id="@+id/textView5" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="To be Updated"
    android:id="@+id/saldo"
    android:textSize="20dp"
    android:layout_alignBottom="@+id/textView5"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="22dp" /></RelativeLayout>

我该怎么办?

我已经在mainactivity(oncreate)中定义了textview 将它定义为RecyclerViewAdapter.ViewHolder onCreateViewHolder

但得到空指针。任何人都可以帮助我。

提前致谢

1 个答案:

答案 0 :(得分:0)

你从哪里获得了

的itemView
itemView.findViewById(R.id.tv_NavTitle);

我认为这就是问题。