我有一个RecyclerView垂直滚动的项目列表.每个列表项都包含精简模式下的Google Maps V2 MapView .我正在利用这个新功能返回位图,而不是一个完整的地图作为替代Google Static Maps API.
MapView的需要你打电话onCreate(),onResume(),onPause(),onDestroy()等从父活动/片段的相应的方法.在哪里RecyclerView.Adapter和/或RecyclerView.ViewHolder?
如何清理回收的MapView,以便内存不会泄漏,同时保持列表清空?
...'精简模式'地图选项,非常适合您想要提供大量较小地图的情况,或者非常适合有意义的互动不切实际的地图,例如列表中的缩略图.
ListItem.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapImageView"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="80dp"
android:layout_height="100dp"
map:liteMode="true"
map:mapType="normal"
map:cameraZoom="15"/>
<!-- ... -->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
RecyclerView.Adapter和ViewHolder
public class NearbyStopsAdapter extends RecyclerView.Adapter<NearbyStopsAdapter.ViewHolder> {
private final Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
MapView map;
public ViewHolder(View view) {
super(view);
map = (MapView) view.findViewById(R.id.mapImageView);
// Should …Run Code Online (Sandbox Code Playgroud) android google-maps google-play-services android-recyclerview google-maps-lite
当我将RecyclerView放在嵌套的scrollview中时,屏幕总是跳到RecyclerView的顶部而不是页面的顶部.这是一个简单的例子.
layout xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="@android:color/holo_blue_dark"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</layout>
Run Code Online (Sandbox Code Playgroud)
虚拟适配器的活动:
public class RecycleViewTestActivity extends AppCompatActivity {
public static class ExampleAdapter extends RecyclerView.Adapter<ExampleViewHolder> {
private Context context;
public ExampleAdapter(Context context) {
this.context = context;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
TextView view = new TextView(context);
view.setText("Test");
return new ExampleViewHolder(view);
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
} …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用了RecyclerView支持,我看到了最奇怪的事情.在我触摸滚动之前,它不会显示任何项目.然后,突然之间,RecyclerView会自行填充.我已经确认填充了支持适配器的列表,并且在触摸事件之前永远不会调用onCreatViewHolder和onBindViewHolder.
以下是我设置recyclerview的方法:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Drawable divider = new ColorDrawable(ProfileInfo.getCurrentProfileColor());
mInboxList.addItemDecoration(new DividerItemDecoration(getActivity(), divider, false));
mInboxList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
mInboxList.setAdapter(new InboxAdapter(getActivity(), new ArrayList<Conversation>()));
new AsyncTask<Void, Void, List<Conversation>{
public List<Conversation> doInBackground(...){
//load the conversations
return conversations;
}
public void onPostExecute(List<Conversation> conversations){
((InboxAdapter) mInboxList.getAdapter()).clear(false);
((InboxAdapter) mInboxList.getAdapter()).addAll(conversations);
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Run Code Online (Sandbox Code Playgroud)
这是我的适配器的要点:
public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.ViewHolder> {
List<Conversation> mConversations; //initialized in contructor
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = mInflater.inflate(R.layout.inbox_item, parent, false);
ViewHolder holder …Run Code Online (Sandbox Code Playgroud) 我们如何知道用户是向下滚动还是向上滚动RecyclerView?
我尝试过RecyclerView#OnScrollListener,它给出了垂直滚动量和滚动状态.滚动状态空闲时,如何在开始拖动和滚动位置时获取最后一个滚动位置.
谢谢.
我试图从我RecyclerView的onRestart方法中删除所有元素,因此项目不会被加载两次:
@Override
protected void onRestart() {
super.onRestart();
// first clear the recycler view so items are not populated twice
for (int i = 0; i < recyclerAdapter.getSize(); i++) {
recyclerAdapter.delete(i);
}
// then reload the data
PostCall doPostCall = new PostCall(); // my AsyncTask...
doPostCall.execute();
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,delete我在其中创建的方法adapter无法正常运行:
在RecyclerAdapter.java中:
public void delete(int position){
myList.remove(position);
notifyItemRemoved(position);
}
public int getSize(){
return myList.size();
}
Run Code Online (Sandbox Code Playgroud)
我认为列表中的每个其他项都会被删除而不是整个列表.
有一个listview它很容易,我只是打电话adapter.clear().
有人可以帮我修改一下代码吗?
我想我应该用,notifyItemRangeRemoved(...,...);但我不确定如何.TIA
如何创建选项菜单,如下面的屏幕截图所示:
单击RecyclerView项目的"更多"-Icon后,应打开选项菜单!
我的尝试是这样的:
@Override
public void onBindViewHolder(Holder holder, int position) {
holder.txvSongTitle.setText(sSongs[position].getTitle());
holder.txvSongInfo.setText(sSongs[position].getAlbum() + " - " + sSongs[position].getArtist());
holder.btnMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "More...", Toast.LENGTH_SHORT).show();
}
});
}
Run Code Online (Sandbox Code Playgroud)
但这会导致问题,因为如果我触摸RecyclerView项目更多按钮,则单击完整项目...
这是我的RecyclerViewOnTouchListener:
public class RecyclerViewOnTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector mGestureDetector;
private OnTouchCallback mOnTouchCallback;
public RecyclerViewOnTouchListener(Context context, final RecyclerView recyclerView, final OnTouchCallback onTouchCallback) {
mOnTouchCallback = onTouchCallback;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我正在试用RecyclerViewAndroid Lollipop中的新功能而且我被卡住了.
我正在尝试接收一个列表,其中包含图标和图标TextView右侧的列表Fragment.
我找到了关于如何设置的精彩教程RecyclerView.我遵循了每一点,只是改变了item_layout.xml我的需要.
该项目构建没有任何错误,但当它在我的设备上启动时,我收到此错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.fredrikaldgard.materialcolors/com.fredrikaldgard.materialcolors.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v7.widget.RecyclerView. setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)'对空对象引用
我试图谷歌问题,但我是Android开发的业余爱好者.
这是我的MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1. get a reference to recyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
// 2. set layoutManger
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// this is data fro recycler view
ItemData itemsData[] = { new ItemData("Indigo",R.drawable.circle),
new ItemData("Red",R.drawable.color_ic_launcher),
new ItemData("Blue",R.drawable.indigo),
new ItemData("Green",R.drawable.circle),
new ItemData("Amber",R.drawable.color_ic_launcher),
new ItemData("Deep Orange",R.drawable.indigo)};
// 3. create …Run Code Online (Sandbox Code Playgroud) java android android-fragments android-5.0-lollipop android-recyclerview
我想隐藏/显示FloatingActionButton滚动RecyclerView.
我的XML布局:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview_eventlist"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_createevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/recyclerview_eventlist"
app:layout_anchorGravity="bottom|right|end"
app:layout_behavior="com.eventizon.behavior.ScrollAwareFABBehavior"
android:src="@drawable/ic_edit"
app:backgroundTint="@color/custom_color_1"
app:borderWidth="0dp" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
DrawerLayout是此布局的父布局.
public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
private static final String TAG = "ScrollAwareFABBehavior";
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
super();
Log.e(TAG,"ScrollAwareFABBehavior");
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout,
FloatingActionButton child, View target, int dxConsumed,
int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
// TODO Auto-generated method stub
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, …Run Code Online (Sandbox Code Playgroud) scroll floating-action-button android-recyclerview android-coordinatorlayout
我需要在recyclerview中检测滚动的开始/结束和方向.滚动侦听器有两种方法:onScrolled()和onScrollStateChanged().滚动开始后调用第一个方法(实际上是调用onScrolled()而不是onScrolling()).第二种方法提供有关状态的信息,但我没有方向信息.我怎样才能实现目标?
我发现了如何使用预览列表项
tools:listitem="@layout/my_item_layout"
Run Code Online (Sandbox Code Playgroud)
但android studio正在将recyclerview预览为垂直列表.有没有办法告诉Android Studio我会附加一个水平,LinearLayoutManager以便它可以预览水平列表?