1

我一直在尝试在我正在从事的项目中使用cardlib lirary。我正在尝试将缩略图与 CustomSource 一起使用。一切都按预期工作。但是,当我在扩展 Card 以查找 R.id.textview_oferta_preco 的类的 setupInnerViewElements 方法中调用 findViewById 时,它返回 null。

贝娄跟着我的课和我的布局。

活动:

public class MenuOpcoes extends AppCompatActivity implements OfertasFragment.OnFragmentInteractionListener, NoticiasFragment.OnFragmentInteractionListener{

private Drawer.Result result = null;
private AccountHeader.Result headerResult = null;

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

    headerResult = new AccountHeader()
            .withActivity(this)
            .withHeaderBackground(R.drawable.header)
            .addProfiles(
                    new ProfileDrawerItem().withName("Oswaldo Roberto Marques").withEmail("oswaldormarques@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile))
            )
            .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                @Override
                public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                    return false;
                }
            })
            .build();

    result = new Drawer()
            .withActivity(this)
            .withTranslucentStatusBar(false)
            .withActionBarDrawerToggle(true)
            .withAccountHeader(headerResult)
            .addDrawerItems(
                    new PrimaryDrawerItem().withName(R.string.drawer_item_listas).withIcon(FontAwesome.Icon.faw_list),
                    new PrimaryDrawerItem().withName(R.string.drawer_item_ofertas).withIcon(FontAwesome.Icon.faw_dollar),
                    new PrimaryDrawerItem().withName(R.string.drawer_item_noticias).withIcon(FontAwesome.Icon.faw_newspaper_o),
                    new SectionDrawerItem().withName(R.string.drawer_item_section_header),
                    new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog)
            )
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
                    if (drawerItem instanceof Nameable) {
                        String opcaoSelecionada = MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes());
                        FragmentTransaction transaction = getFragmentManager().beginTransaction();
                        if (opcaoSelecionada.equals("Ofertas")) {
                            Toast.makeText(MenuOpcoes.this, MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()), Toast.LENGTH_SHORT).show();
                            OfertasFragment ofertasFragment = new OfertasFragment();
                            transaction.replace(R.id.home_layout_container, ofertasFragment);
                            transaction.addToBackStack("ofertas");
                        }
                        if (opcaoSelecionada.equals(R.string.drawer_item_noticias)) {
                            Toast.makeText(MenuOpcoes.this, MenuOpcoes.this.getString(((Nameable) drawerItem).getNameRes()), Toast.LENGTH_SHORT).show();
                            NoticiasFragment noticiasFragment = new NoticiasFragment();
                            transaction.replace(R.id.home_layout_container, noticiasFragment);
                            transaction.addToBackStack("noticias");
                        }
                        transaction.commit();
                    }
                }
            }).build();

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_menu_opcoes, 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();

    //noinspection SimplifiableIfStatement
    //if (id == R.id.action_settings) {
    //    return true;
    //}

    //return super.onOptionsItemSelected(item);

    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;


        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
    //handle the back press :D close the drawer first and if the drawer is closed close the activity
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        this.finish();
    }
}

@Override
public void onFragmentInteraction(Uri uri) {

}

自定义来源:

public class OfertasCardCustomSource extends Card {


public OfertasCardCustomSource(Context context) {
    super(context);
    init();
}

public OfertasCardCustomSource(Context context, int innerLayout) {
    super(context, innerLayout);
    init();
}

private void init() {
    CardHeader header = new CardHeader(getContext());
    header.setButtonOverflowVisible(true);
    header.setTitle("Coca Cola 2,5L");
    addCardHeader(header);

    CardThumbnail thumbnail = new CardThumbnail(getContext());
    thumbnail.setDrawableResource(R.drawable.coca_cola_25l);
    addCardThumbnail(thumbnail);
}

@Override
public void setupInnerViewElements(ViewGroup parent, View view) {

    TextView title = (TextView) parent.findViewById(R.id.textview_oferta_preco);
    title.setText("R$ 4,25");


    TextView subtitle = (TextView) parent.findViewById(R.id.textview_oferta_mercado);
    subtitle.setText("Mercado Exemplo");


    RatingBar mRatingBar = (RatingBar) parent.findViewById(R.id.carddemo_gplay_main_inner_ratingBar);


    mRatingBar.setNumStars(5);
    mRatingBar.setMax(5);
    mRatingBar.setStepSize(0.5f);
    mRatingBar.setRating(4.7f);
}
}

片段布局:

<ScrollView
    android:id="@+id/card_scrollview"
    style="@style/carddemo_default_container_padding"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <!-- Custom Source Thumbnail-->
        <TextView
            style="@style/Theme.Carddemo.TitleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/secao_ofertas"/>


        <it.gmariotti.cardslib.library.view.CardViewNative
            android:id="@+id/carddemo_thumb_customsource"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/card_external"
            card:card_layout_resourceID="@layout/native_card_thumbnail_layout"/>
        <!-- Empty view-->
        <View
            android:layout_width="match_parent"
            android:layout_height="15dp"/>

    </LinearLayout>
</ScrollView>

自定义源布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
>

<RatingBar
    android:id="@+id/carddemo_gplay_main_inner_ratingBar"
    style="@style/carddemo_myapps_main_inner_ratingbar"
    android:numStars="5"
    android:stepSize="1.0"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/textview_oferta_preco"
    android:layout_width="wrap_content"
    android:textSize="12sp"
    android:textColor="@android:color/holo_green_light"
    android:layout_alignParentRight="true"
    android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/textview_oferta_mercado"
    android:textSize="12sp"
    android:textColor="#BBB"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

我不知道该怎么办。我正在使用安卓工作室。谢谢你。

4

1 回答 1

0

扩展时Card,您必须super通过传递要膨胀的布局引用来调用。

我在您的OfertasCardCustomSource类中看到您有两个构造函数,其中一个接收一个innerLayout正确传递给超类的变量,另一个接收不设置布局的构造函数。

您的问题可能是以下之一:

  • 您没有在该构造函数中设置布局:您应该执行诸如调用之类的操作super(context, R.layout.the_layout_of_your_card)-查看此参考以获取更详细的示例
  • 您没有调用在innerLayout任何地方接收的构造函数

我建议您做的是删除不接收要膨胀的布局的构造函数,或者完全按照我在上面留下的链接中所做的那样传递默认值。

于 2015-05-04T14:46:00.757 回答