小编azi*_*ian的帖子

如何更改RecyclerView中所有项目的布局?

我有一个RecyclerView。在适配器中,我有一个布尔标志来确定应为所有项目使用哪种布局:

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

    private boolean mIsSecondModeEnabled;

    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mIsSecondModeEnabled ?
         R.layout.layout_second_mode_item :
         R.layout.layout_first_mode_item, parent, false);
        return new MyHolder(view);
    }

    public void setModeEnabled(boolean enabled) {
        mIsSecondModeEnabled = enabled;
        notifyDataSetChanged();
    }
}
Run Code Online (Sandbox Code Playgroud)

该代码无法完全正确地工作。当我打电话给setModeEnabled(true)一些显示layout_first_mode_item布局的项目时。

如何实现呢?

java android android-layout android-view android-recyclerview

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

在运行时移除 FLAG_TRANSLUCENT_STATUS

我有MainActivity很多Fragments 并且在一个特定的片段中我想在系统栏后面绘制,所以我在运行时应用以下标志:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
Run Code Online (Sandbox Code Playgroud)

我现在想要的是当用户退出Fragment并输入另一个以删除该标志时,状态栏后面没有内容。我尝试将 null 作为参数传递给setFlags()方法,但这给出了错误。我已经搜索remove()unSet()方法,但不存在。那么我应该如何删除另一个标志Fragment

java flags android android-windowmanager android-window

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

从 Android 中的过渡动画中排除 BottomNavigation

我一直在四处搜索,但找不到有助于解决此特定问题的答案。我的应用程序有一个自定义的滑入、滑出效果,如下所示:

Intent intent = new Intent(getApplicationContext(), MyActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
Run Code Online (Sandbox Code Playgroud)

问题

问题是我在所有活动中都包含了一个底部导航,我不希望它被动画化,我想排除它。

在此处输入图片说明

我正在努力实现的目标

我想从动画中排除底部导航。或者,换句话说,我怎样才能在转换之间只为内容设置动画?

在此处输入图片说明

编辑:我已经尝试过使用共享元素,但我希望它在 API 21 下工作。

animation android android-animation android-layout android-view

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

在分段上传请求中实现ProgressDialog

我使用以下方法将图像从Android上传到服务器.

 public void uploadMultipart() {
        //getting name for the image
        String name = editText.getText().toString().trim();

        //getting the actual path of the image
        String path = getPath(filePath);


        progress = ProgressDialog.show(this, "Subiendo imagen",
                "Por favor, espere...", true);
        //Uploading code
        try {
            String uploadId = UUID.randomUUID().toString();

            //Creating a multi part request
            new MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload(path, "image") //Adding file
                    .addParameter("name", name) //Adding text parameter to the request
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); //Starting the upload


        } catch (Exception exc) {
            Toast.makeText(this, exc.getMessage(), Toast.LENGTH_SHORT).show();
        }

    } …
Run Code Online (Sandbox Code Playgroud)

java upload android multipart progressdialog

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

Android RecyclerView适配器在单元测试时给出null

我正在尝试使用AndroidJunit4测试RecyclerView,这是我的测试代码:

package com.kaushik.myredmart.ui;
// all includes
@RunWith(AndroidJUnit4.class)
public class ProductListActivityTest {

    @Rule
    public ActivityTestRule<ProductListActivity> rule  = new  ActivityTestRule<>(ProductListActivity.class);

    @Test
    public void ensureListViewIsPresent() throws Exception {
        ProductListActivity activity = rule.getActivity();
        View viewByIdw = activity.findViewById(R.id.productListView);
        assertThat(viewByIdw,notNullValue());
        assertThat(viewByIdw, instanceOf(RecyclerView.class));
        RecyclerView productRecyclerView = (RecyclerView) viewByIdw;
        RecyclerView.Adapter adapter = productRecyclerView.getAdapter();
        assertThat(adapter, instanceOf(ProductAdapter.class));

    }
}
Run Code Online (Sandbox Code Playgroud)

我正面临检查适配器的问题.虽然productRecyclerView传递非null测试和RecyclerView的实例,但它在最后一行中跟随错误:

java.lang.AssertionError:
Expected: an instance of com.kaushik.myredmart.adapter.ProductAdapter
but: null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at com.kaushik.myredmart.ui.ProductListActivityTest.ensureListViewIsPresent(ProductListActivityTest.java:45)
Run Code Online (Sandbox Code Playgroud)

代码中有什么问题?

java android recycler-adapter android-recyclerview android-junit

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

为什么minHeight属性在WebView Android中不起作用?

这个问题已在这里提出,但它没有解决方案.

我有一个WebView.我想将最小高度设置为WebViewusing minHeight属性,但它不起作用.相同的属性适用于Button.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anshul.webview.WebActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="400dp"></WebView>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:minHeight="150dp"
    android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/>
Run Code Online (Sandbox Code Playgroud)

显然,从下图中,WebView不支持该minHeight属性.有谁知道这个问题的解决方案?

在此输入图像描述

java android webview android-layout view-hierarchy

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

工具:RecyclerView项目的文本

我知道你设置的时候

tools:text="Sample text"
Run Code Online (Sandbox Code Playgroud)

在a中TextView,您将在Android Studio的预览模式下看到示例文本,但不会在实际应用中看到.我想为a中的项目做这个RecyclerView,但我似乎无法做到.这是我到目前为止所做的:

在RecyclerView(名为content_feed)中:

tools:listitem="@layout/cell_feed"
Run Code Online (Sandbox Code Playgroud)

在单元格中(名称为cell_feed):

tools:showIn="@layout/content_feed"
Run Code Online (Sandbox Code Playgroud)

这是xml文件:

cell_feed.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"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_feed_cell"
    android:layout_marginLeft="@dimen/margin_feed_cell"
    android:layout_marginRight="@dimen/margin_feed_cell"
    android:orientation="horizontal"
    tools:showIn="@layout/content_feed">

    <LinearLayout
        android:id="@+id/timeLayouts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="@dimen/alpha_feed_secondary_text"
            android:textSize="@dimen/size_feed_secondary_text"
            android:id="@+id/startTimeText"
            tools:text="8:00 AM"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="@dimen/alpha_feed_secondary_text"
            android:textSize="@dimen/size_feed_secondary_text"
            android:id="@+id/endTimeText"
            tools:text="10:00 AM"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_feed_cell_text"
        android:layout_toRightOf="@+id/timeLayouts"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/margin_bottom_feed_cell_title"
            android:textSize="@dimen/size_feed_cell_title"
            android:textStyle="bold"
            android:id="@+id/titleText"
            tools:text="Event title"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="@dimen/alpha_feed_secondary_text"
            android:textSize="@dimen/size_feed_secondary_text"
            android:id="@+id/captionText"
            tools:text="Event caption"/>
    </LinearLayout>

    <CheckBox
        android:layout_width="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android android-studio android-recyclerview android-tools-namespace android-layout-editor

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

AmbiguousViewMatcherException多个RecyclerViews

我有一个Fragment包含RecyclerView。但是,由于其中包含很多元素,因此Fragment我想向上滑动列表以查看并检查其中的所有元素Fragment

早期,此方法对我有帮助,但现在由于某种原因它不起作用:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp())
Run Code Online (Sandbox Code Playgroud)

我的项目中有很多RecyclerView相同的id

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"/>
Run Code Online (Sandbox Code Playgroud)

同样在测试中,我写了这样的东西:

onView(allOf( withId(R.id.recyclerView), isDisplayed()))
onView(withId(R.id.recyclerView)).perform(swipeUp())
Run Code Online (Sandbox Code Playgroud)

但是仅在第二行捕获了错误。

android.support.test.espresso.AmbiguousViewMatcherException:'with id:com.fentury.android:id/recyclerView'匹配层次结构中的多个视图。问题视图在下面标有“ **** MATCHES ****”。

android swipe android-fragments android-espresso android-recyclerview

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

在没有Window的情况下更改状态栏颜色

我想操纵状态栏(ex.color)但在后台.我正在使用前台服务来执行此操作.因此,没有窗口,因为它在后台发生,特别是没有活动.但是,功能

public abstract void setStatusBarColor (int color)
Run Code Online (Sandbox Code Playgroud)

由抽象类Window调用:https://developer.android.com/reference/android/view/Window.html

所以因为它是抽象的我无法启动它而且我不能使用getWindow()因为我没有实现一个活动类.以下答案使用Activity.还有其他方法可以实现吗? 如何更改android中的状态栏颜色

android statusbar android-windowmanager android-statusbar android-window

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

如何更改 CalendarView 中的月份文本颜色

我在我的项目中使用CalendarView,我需要对其进行特殊的设计。我可以更改 CalendarView 中所有内容的颜色,但只能更改日历顶部月份的颜色。我尝试了xml中的所有属性。如何更改月份铭文和箭头的颜色?

<CalendarView
        android:id="@+id/calendarID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="Heh"
        android:maxDate="31/12/2018"
        android:minDate="09/01/2012"
        android:showWeekNumber="false"
        android:focusedMonthDateColor="@android:style/TextAppearance.Small"
        android:background="#004366"
        android:unfocusedMonthDateColor="@android:style/TextAppearance.Small"
        android:dateTextAppearance="@android:style/TextAppearance.Small"
        android:weekDayTextAppearance="@android:style/TextAppearance.Small"
        android:weekSeparatorLineColor="@android:style/TextAppearance.Small"
        android:selectedWeekBackgroundColor="@android:style/TextAppearance.Small"
        />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

 LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
            (Context.LAYOUT_INFLATER_SERVICE);

    LinearLayout ll= (LinearLayout)inflater.inflate(R.layout.calendar_moi, null, false);

    CalendarView cv = (CalendarView) ll.getChildAt(0);
    Long maxData = System.currentTimeMillis();
    cv.setMaxDate(maxData);

    cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {


        }
    });

    new AlertDialog.Builder(ItemClassActivity.this)
            .setTitle("")
            .setMessage("")
            .setView(ll)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view calendarview android-styles

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