我希望使用RecyclerView
将CardView
作为数据列表显示。但是我得到了一个空白屏幕(RecyclerView
部分)。我想要显示一些文本和图片。
CheeseDetailActivityexp.java
public class CheeseDetailActivityexp extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,LocationListener {
public static final String EXTRA_NAME = "cheese_name";
private static LatLng def_latlng=new LatLng(12.0,77.0);
private LatLng mLocation;
private Marker mLocationMarker;
private SupportMapFragment mMapFragment;
private GoogleMap mMap;
private boolean mIsNeedLocationUpdate = true;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManager;
CardAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
setUpMapIfNeeded();
Intent intent = getIntent();
final String cheeseName = intent.getStringExtra(EXTRA_NAME);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("");
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CardAdapter();
mRecyclerView.setAdapter(mAdapter);
// loadBackdrop();
}
/* @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.main_activity_mapview))
.getMap();
if (mMap != null) {
setUpMap();
}
}}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
// Check if we were successful in obtaining the map.
LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
if (!(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)))
moveToLocation(def_latlng);
LatLng update = getLastKnownLocation();
if (update != null) {
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(12.845351,77.66105)).zoom(12).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(12.845351,77.66105)).zoom(20).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private LatLng getLastKnownLocation() {
return getLastKnownLocation(true);
}
private LatLng getLastKnownLocation(boolean isMoveMarker) {
LocationManager lm = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_LOW);
String provider = lm.getBestProvider(criteria, true);
if (provider == null) {
return null;
}
Location loc = lm.getLastKnownLocation(provider);
if (loc != null) {
LatLng latLng = new LatLng(loc.getLatitude(), loc.getLongitude());
return latLng;
}
return null;
}
private void moveToLocation(Location location) {
if (location == null) {
return;
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
moveToLocation(latLng);
}
private void moveToLocation(LatLng latLng) {
moveToLocation(latLng, true);
}
private void moveToLocation(LatLng latLng, final boolean moveCamera) {
if (latLng == null) {
return;
}
mLocation = latLng;
/* mListView.post(new Runnable() {
@Override
public void run() {
if (mMap != null && moveCamera) {
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.fromLatLngZoom(mLocation, 11.0f)));
}
}
});*/
}
@Override
public void onLocationChanged(Location location) {
if (mIsNeedLocationUpdate) {
moveToLocation(location);
}
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setNumUpdates(1);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void onStart() {
super.onStart();
// Connect the client.
mGoogleApiClient.connect();
}
@Override
public void onStop() {
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
}
}
适配器模式代码
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
ArrayList<BusRouteNameDetails> mItems;
public CardAdapter() {
super();
mItems = new ArrayList<>();
BusRouteNameDetails routename = new BusRouteNameDetails();
routename.setName("GopalanMall-Siemens");
routename.setNumber("S4");
mItems.add(routename);
routename = new BusRouteNameDetails();
routename.setName("GopalanMall-Siemens");
routename.setNumber("S4");
mItems.add(routename);
routename = new BusRouteNameDetails();
routename.setName("GopalanMall-Siemens");
routename.setNumber("S4");
mItems.add(routename);
routename = new BusRouteNameDetails();
routename.setName("GopalanMall-Siemens");
routename.setNumber("S4");
mItems.add(routename);
routename = new BusRouteNameDetails();
routename.setName("GopalanMall-Siemens");
routename.setNumber("S4");
mItems.add(routename);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.busroutename_list, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
BusRouteNameDetails routename = mItems.get(i);
viewHolder.tvName.setText(routename.getName());
viewHolder.tvRoutename.setText(routename.getNumber());
}
@Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvName;
public TextView tvRoutename;
public ViewHolder(View itemView) {
super(itemView);
tvName = (TextView)itemView.findViewById(R.id.RouteName1);
tvRoutename = (TextView)itemView.findViewById(R.id.Route_src_dest);
}
}
}
getter/setters操作
public class BusRouteNameDetails {
private String mName;
private String mNumber;
public String getName() {
return mName;
}
public void setName(String name) {
this.mName = name;
}
public String getNumber() {
return mNumber;
}
public void setNumber(String Number) {
this.mNumber = Number;
}
}
Mainactivity *.xml
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<fragment
android:id="@+id/main_activity_mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
class="com.google.android.gms.maps.SupportMapFragment"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- <android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="400dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/ic_discuss"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
最终的CardView XML。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="3dp"
android:layout_marginTop="9dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="9dp"
card_view:cardElevation="0.01dp"
android:layout_marginBottom="0dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/border">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:padding="2dp"
android:src="@drawable/rsz_bus_symbol"/>
<TextView
android:padding="2dp"
android:textStyle="bold"
android:id="@+id/RouteName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:paddingLeft="10dp"
android:textStyle="bold"
android:id="@+id/Route_src_dest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
我哪里做错了吗?
RecyclerView
放在NestedScrollView
里面的原因是什么? - random