小编Ren*_*ier的帖子

嵌套的scrollview + recyclerview,奇怪的自动滚动行为

在一个视图分页器中,我有几个片段,其中一个片段使用带有标题和recyclerview的嵌套scrollview:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.m360.android.fragment.Members.MemberDetailsFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="20dp">

        <header/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="0dp" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)

标签"header"代表了一个复杂的布局,我不想在这里发布,因为它会大量延伸代码.

当我在标签之间切换时,它会将海峡滚动到回收站视图.标题是隐藏的,我必须向上滚动才能看到它.

关于是什么原因的任何想法?如果可以避免,我不想在我的适配器中使用类型.

android android-recyclerview android-nestedscrollview

31
推荐指数
2
解决办法
1万
查看次数

AppBarLayout + TabLayout + CollapsingToolbarLayout + SwipeToRefresh

我遇到了很多与我的问题有关的问题,但我发现没有人想要我正在寻找的行为.

我想要一个带有标签和应用栏的视图寻呼机,当所包含的片段可滚动并滚动(朝向底部)时,我希望应用栏消失,但是当向上滚动时,将标签留下以重新显示.其中一些片段包含SwipeToRefresh布局(有时会导致问题).我尝试了很多配置,总是会出错.

现在我没有崩溃效果(它总是隐藏)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainPagerActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|enterAlways">


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabGravity="fill"
                app:tabIndicatorHeight="6dp"
                app:tabMode="fixed" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/main_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/app_bar_layout"
        tools:context=".MainPagerActivity"

        />

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_menu_edit" />
Run Code Online (Sandbox Code Playgroud)

和我的一个片段(滚动的一个):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.m360learning.app.fragment.MainPagerNewsFeed">


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout …
Run Code Online (Sandbox Code Playgroud)

android swiperefreshlayout android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout

12
推荐指数
1
解决办法
8103
查看次数

RealmList序列化问题(Realm/Gson/Intent)

我在我的项目中使用了Retrofit,Gson和Realm.我有这个课程Example需要Serializable.没有Realm,我会这样写:

public class Example implements Serializable {
    @SerializationName("users")
    private List<String> users

    //... getters and setters
}
Run Code Online (Sandbox Code Playgroud)

Realm开始发挥作用Example(请注意,出于兼容性原因,getter和setter是这样的):

public class Example extends RealmObject implement Serializable {

    @SerializedName("users")
    private RealmList<RealmString> users;

    public ArrayList<String> getUsers() {
        ArrayList<String> array = new ArrayList<>();
        for (RealmString rs : users) {
            array.add(rs.getValue());
        }
        return array;
    }

    public void setUsers(ArrayList<String> users) {
        RealmList<RealmString> array = new RealmList<>();
        for (String rs : users) {
            array.add(new RealmString(rs));
        }
        this.users = array;
    }

}
Run Code Online (Sandbox Code Playgroud)

RealmString是: …

java serialization android realm

6
推荐指数
1
解决办法
1972
查看次数

如何在TabLayout中检测已选择的选项卡上的单击

我的问题已经回答了好几次当它是一个TabActivitytabWidgets.但我无法找到关于相对新TabLayout观点的任何内容.

这很简单,比如facebook,我想点击已经选中的标签,让我的列表向上滚动直到它的开始.但我无法弄清楚我的听众放在哪里.

我尝试了TabLayout自己,mTabLayout.getChildAt()然后继续TabLayout.getTabAt().getCustomView().


编辑:更正:正如CommonsWare在其答案的评论中提到的那样,我不得不重写"onTabSelected"的行为.

mTabLayout = (TabLayout) findViewById(R.id.tabs);
mTabLayout.setupWithViewPager(mViewPager);

for (int i = 0; i < mTabLayout.getTabCount(); i++) {
    mTabLayout.getTabAt(i).setIcon(tabIcons[i]);
}

mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {
        if (tab.getPosition() == PAGE_FEED) {
                ((PagerNewsFeedFragment) mViewPagerAdapter.getRegisteredFragment(PAGE_FEED)).scrollToTop();
            }
    }
});
Run Code Online (Sandbox Code Playgroud)

谢谢 !

android android-tablayout

5
推荐指数
1
解决办法
4320
查看次数

从Web视图中的本地私有目录加载图像

内容:

我的应用有时显示“ Sheets”,它们是包含文本和图像的html文件:

<p>image description </p>
<p><img src="/api/medias/get/videos/56afad6447eba61c8099544b?thumbnail=true&amp;company=4f86d0a9d655ab9794f5d1d2&amp;fullSizedCover=true" 
    alt="" 
    data-id="56afad6447eba61c8099544b" 
    data-type="video" data-width="640" data-height="1136" /></p>
Run Code Online (Sandbox Code Playgroud)

然后我用 body.loadDataWithBaseURL(<my api url>, body, "text/html; charset=UTF-8", null, null);

我认为这无关紧要,但是为了防万一,我有一个包含CSS和JavaScript的模板主体。js脚本检测点击的图像并将“数据ID”传输到android方法(通过JavascriptInterface)。在这种情况下,它将打开视频播放器并播放。


问题:

我的应用程序允许用户下载这些表格以进行离线脱机处理。因此,我下载了html,然后将图像下载到了本地私有目录(Context.getFileDir()),并更改了html的src,将“缩略图”源设置为我下载的图像:

<p>video</p>
<p><img src="69c623955ecb5bd414f908cd383f3809.jpg" 
    alt="" 
    data-id="56afad6447eba61c8099544b" 
    data-type="video" data-width="640" data-height="360" /></p>
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何将基本URL放置到Webview中,以获取预期的行为(即:显示下载的图像)。

我想Context.getFileDir().getAbsolutePath()content://com.android.htmlfileprovider和其他一些。

我应该做些不同的事情吗?

非常感谢,


这很好用:

Picasso.with(iv.getContext()).load(new File(getContext().getFilesDir() + "/" + "69c623955ecb5bd414f908cd383f3809.jpg")).into(iv);

的html:

<p>video</p>
<p><img src="69c623955ecb5bd414f908cd383f3809.jpg" alt="" data-id="56afad6447eba61c8099544b" data-type="video" data-width="640" data-height="360" /></p>
Run Code Online (Sandbox Code Playgroud)

我的基本网址: baseUrl = Uri.fromFile(getContext().getFilesDir()).toString();

最后: webview.loadDataWithBaseURL(baseUrl, body, "text/html; charset=UTF-8", null, null);

android android-webview

5
推荐指数
1
解决办法
1096
查看次数

使用OkHttp下载损坏的文件

我写下载文件的方法总是会产生损坏的文件.

public static String okDownloadToFileSync(final String link, final String fileName, final boolean temp, DownloadStatusManager statusManager, ErrorDisplayerInterface errorDisplayerInterface) {

    Request request = new Request.Builder()
            .url(link)
            .build();


    OkHttpClient client = Api.getInstance().getOkHttpClient();
    OutputStream output = null;
    InputStream input = null;

    try {

        Response response = client.newCall(request).execute();

        //Add the file length to the statusManager
        final int contentLength = Integer.parseInt(response.header("Content-Length"));
        if (statusManager != null) {
            statusManager.add(Hash.md5(link), contentLength);
        }

        //Get content type to know extension
        final String contentType = response.header("Content-Type");
        final String ext = contentTypeMap.get(contentType);

        Log.i(TAG, …
Run Code Online (Sandbox Code Playgroud)

java android download okhttp okhttp3

5
推荐指数
1
解决办法
816
查看次数

MotionLayout OnSwipe touchRegionId 与 OnClick targetId 冲突

我的 MotionLayout 中有这个转换。

<Transition
    android:id="@+id/transition_validate"
    motion:constraintSetEnd="@id/endRight"
    motion:constraintSetStart="@id/start"
    motion:duration="400"
    motion:motionInterpolator="easeOut">
  <OnClick
      motion:clickAction="transitionToEnd"
      motion:targetId="@id/trueButton" />
  <OnSwipe
      motion:dragDirection="dragRight"
      motion:touchAnchorId="@id/card"
      motion:touchRegionId="@id/card"
      motion:touchAnchorSide="right" />
Run Code Online (Sandbox Code Playgroud)

中间的矩形是带有cardid 的cardView。屏幕左右按钮分别为trueButton/falseButton

我的问题是,在我添加motion:touchRegionId="@id/card"到 onSwipe 后,我不能再点击1trueButton ,即上面的部分card来启动动画(它实际上在附加的屏幕上为 false 而不是 true,但你明白了),我必须点击 3 即,该部分trueButton不在卡片上方。

在此输入图像描述

你见过这样的东西吗?您知道如何限制 onSwipe 触摸card但单击按钮启动动画吗?

谢谢

android android-motionlayout

5
推荐指数
0
解决办法
571
查看次数

Retrofit,如何用可变键解析JSON对象

首先我知道我的头衔不好,因为我没有想出更好的,我愿意接受建议。

我正在使用改造从这种 api 获取数据: @GET("users/{userid}")

它工作正常,我很满意,问题是当我使用@POST("users/widget")ID 列表调用相同的 api 时。我有以下答案:

{
  "long_hash_id": {
    "_id": "long_hash_id"
    .......
  },
  "long_hash_id": {
    "_id": "long_hash_id",
    .....
  },
  ........
}
Run Code Online (Sandbox Code Playgroud)

“long_hash_id”通常是“525558cf8ecd651095af7954”,它对应于附加到它的用户的ID。

没用retrofit的时候,我用流模式的Gson,把每个用户一一获取。但我不知道如何告诉改造。

希望我很清楚,并在此先感谢您。

- - - - - - 解决方案 :

我是这样制作我的界面的:

@FormUrlEncoded
@POST(AppConstants.ROUTE_USER_GROUP)
Call<Map<String,User>> getUsers( @Field("ids") List<String> param, @QueryMap Map<String,String> options);
Run Code Online (Sandbox Code Playgroud)

我只是给了我的 ArrayList id。非常感谢

android json gson retrofit

3
推荐指数
1
解决办法
1079
查看次数

服务构造函数中的Context是null?

我的目标是拥有一个后台服务,每天更新两次相当大量的数据(更新约5分钟,可能更多).

所以我有一个启动此服务的GcmTaskService:

public class SyncOfflineCoursesService extends Service {

    private final IBinder mBinder = new MonBinder();
    private final SharedPreferenceManagerToReplace sharedPreferenceManager;

    public SyncOfflineCoursesService() {

        sharedPreferenceManager = new SharedPreferenceManagerToReplace(this); //crash on this line

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class MonBinder extends Binder {
        SyncOfflineCoursesService getService() {
            return SyncOfflineCoursesService.this;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

SharedPreferenceManagerToReplace :

public class SharedPreferenceManagerToReplace {
    private final SharedPreferences prefs;

    public SharedPreferenceManagerToReplace(Context context) { …
Run Code Online (Sandbox Code Playgroud)

android android-service android-context

3
推荐指数
1
解决办法
2132
查看次数

CollapsingToolbarLayout + TabLayout,奇怪的工具栏标题边距

我有一个srange工具栏标题行为,我怀疑是有效的.

崩溃了:

折叠的工具栏

延长:

扩展工具栏

最奇怪的部分是

应用:titleMarginTop

对标题的放置没有任何影响.

这是我的Xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.m360learning.app.activity.UserDetailsActivity">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:background="@drawable/background_navigation_drawer"
                app:layout_collapseMode="parallax">
                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerInside" />

            </RelativeLayout>


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:gravity="top"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

                 />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                app:tabIndicatorColor="@color/colorAccent" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.谢谢

---编辑:如果我将工具栏大小设置为"actionbarsize" …

android android-collapsingtoolbarlayout

2
推荐指数
2
解决办法
4821
查看次数