私が取り組んでいるプロジェクトで、cardslib ライブラリを使用しようとしています。CustomSource で Thumbnail を使用しようとしています。すべてが期待どおりに機能します。ただし、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"/>
どうすればいいのかわからない。Androidスタジオを使用しています。ありがとうございました。