Пользовательские инъекции ActionBar и RoboGuice

Я создал пользовательский ActionBar для своего проекта, в который я хочу добавить элементы представления для моего ActionBar, используя (InjectView). Однако это не работает. С другой стороны, findViewById работает нормально.

Я думаю, что эта проблема связана с тем, что ActionBar создается после того, как MainActivity закончила свою разметку. Однако это также не имеет смысла, так как я все еще могу использовать findViewbyId для получения представлений.

Вот определение XML для панели действий:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/activityTitleTxt"
            android:layout_alignParentTop="false"
            android:layout_gravity="center"
            android:textColor="@color/white" />

    </LinearLayout>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:showDividers="middle"
        android:id="@+id/filterRadioGroup"
        android:layout_toLeftOf="@+id/layoutRadioGroup">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All"
            android:id="@+id/selectAllBtn"
            android:background="@null"
            android:button="@null"
            android:textColor="@color/white"
            android:layout_marginRight="5dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@android:drawable/btn_star"
            android:id="@+id/selectFavBtn" />
    </RadioGroup>

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/layoutRadioGroup"
        android:layout_toLeftOf="@+id/shareSearchLayout"
        android:gravity="center">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listViewBtn"
            android:background="@null"
            android:button="@drawable/ic_action_view_as_list"
            android:gravity="center"
            android:checked="true" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tileViewBtn"
            android:background="@null"
            android:button="@drawable/ic_action_picture"
            android:gravity="center" />
    </RadioGroup>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="0dp"
        android:id="@+id/shareSearchLayout">

        <SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchView"
            android:layout_gravity="center" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/shareBtn"
            android:background="@android:color/transparent"
            android:src="@android:drawable/ic_menu_share"
            android:layout_gravity="center" />

    </LinearLayout>

</RelativeLayout>

ActionBar View:

public class ActionBarView extends LinearLayout {

    private MainActivity activity;

    @InjectView(R.id.activityTitleTxt)
    private TextView activityTitleTxt;
    @InjectView(R.id.filterRadioGroup)
    private RadioGroup filterGroup;
    @InjectView(R.id.layoutRadioGroup)
    private RadioGroup layoutGroup;
    @InjectView(R.id.searchView)
    private SearchView searchView;
    @InjectView(R.id.shareBtn)
    private ImageButton shareBtn;

    public ActionBarView(MainActivity activity) {
        super(activity);

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.action_bar_view, this);

//        this.activityTitleTxt = (TextView) findViewById(R.id.activityTitleTxt);
////        this.selectAllTxt = (TextView) findViewById(R.id.selectAllTxt);
////        this.selectFavBtn = (ImageButton) findViewById(R.id.selectFavBtn);
//        this.filterGroup = (RadioGroup) findViewById(R.id.filterRadioGroup);
//        this.layoutGroup = (RadioGroup) findViewById(R.id.layoutRadioGroup);
//        this.searchView = (SearchView) findViewById(R.id.searchView);
//        this.shareBtn = (ImageButton) findViewById(R.id.shareBtn);

        this.activity = activity;
        setListeners();
    }

    private void setListeners() {
        this.filterGroup.setOnCheckedChangeListener(activity);
        this.layoutGroup.setOnCheckedChangeListener(activity);
    }

    public void setActivityTitle(String title){
        this.activityTitleTxt.setText(title);
    }
}

Вот как я прикрепляю ActionBar к основному виду деятельности:

public class MainActivity extends BaseActivity implements View.OnClickListener,RadioGroup.OnCheckedChangeListener{


@InjectView(R.id.quoteGridView)
private AbsListView absListView;
@InjectView(R.id.header1Txt)
private TextView header1Txt;
@InjectView(R.id.header2Txt)
private TextView header2Txt;
@InjectView(R.id.header3Txt)
private TextView header3Txt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // ActionBar configurations
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);

    ActionBarView actionBarView = new ActionBarView(this);
    actionBarView.setActivityTitle("Quote");
    actionBar.setCustomView(actionBarView);

2 ответа

Решение

Поскольку RoboGuice внедряет представления в setContentView(R.layout.activity_main), я смог решить эту проблему, переместив actionBar.setCustomView(actionBarView) перед setContentView(R.layout.activity_main).

InjectView прикрепить представление к определенному контексту (Activity), в вашем случае вы не можете внедрить представление в класс, который расширяет LinearLayoutда, это работает с findViewbyId потому что эта функция связывает view в определенный контекст.

Другие вопросы по тегам