小编Pha*_*tom的帖子

RecyclerView与GridLayoutManager和第一个元素具有不同的viewHolder

我需要在两行上创建一个带有GridLayoutManager的recyclerView,并且第一个元素要比其余元素大.结果应如下所示: 在此输入图像描述

我设法实现了这个目标,但是以一种非传统的方式.在我的recyclerView适配器中,我使用不同的viewHolder作为第一个元素,一个更大的元素.那将是一个很好的解决方案,但第二个元素将低于第一个元素.所以我做了一个技巧,给予recyclerView一个与第一个元素相同的固定高度,这样第一个元素和第二个元素会重叠,我只会让第二个元素的可见性消失.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    switch (viewType) {
        case 0:
            final View view = inflater.inflate(R.layout.big_item, parent, false);
            return new BigViewHolder(view);
        case 2:
            final View view2 = inflater.inflate(R.layout.normal_item, parent, false);
            return new NormalViewHolder(view2);
        default:
            return null;
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (position == 1) {
        holder.itemView.setVisibility(View.GONE);
    }}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return 0;
    } else return 2;
}
Run Code Online (Sandbox Code Playgroud)

但我并不特别喜欢这种方法.有人对此有更好的想法吗?

android android-layout gridlayoutmanager android-recyclerview

7
推荐指数
1
解决办法
2916
查看次数

等到片段已添加到UI

在我的应用程序中,我得到了一个点,在横向模式下,我需要附加两个片段.为了做到这一点,第二个片段需要等到第一个片段被添加(添加)之后才被添加.原因是第一个片段需要执行第二个片段所需的功能.我设法通过一个线程来做到这一点,但在这种情况下它只等待指示的时间,在附加第二个片段之前,如果第一个片段没有在给定的时间附加,应用程序将粉碎,因为第二个片段没有必要的数据.

任何更好的做法(例子)然后是下面的代码(比如等到第一个片段被连接,而且在某个时间间隔没有)?:

getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.mainContent, fragment).commit();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        wait(1000);
                    }
                } catch (InterruptedException e) {

                }
                if (isLandscape) {
                openSecondFragment(mIndex, R.id.rightConent);
                }
            }
        };
        thread.start();
Run Code Online (Sandbox Code Playgroud)

非常感激.

我需要在第一个片段中执行处理程序:

@SuppressLint("HandlerLeak")
protected void loadAccountList() {

    LoadAccountList loadAccountListThread = new LoadAccountList(new Handler() {

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case LOAD_SUCCESSFUL_CODE:
                items = (ArrayList<Account>) msg.obj;
                break;
            case LOAD_FAILED_IOEXCEPTION_CODE:
                getActivity().showDialog(ERROR_DIALOG);
                break;
            default:
                break;
            }
        }
    });
    loadAccountListThread.start();
Run Code Online (Sandbox Code Playgroud)

multithreading android android-fragments

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

在适配器中的多个 RecyclerViews 上同步滚动

我想要实现的水平RecyclerView垂直内RecyclerView

最终结果应该是这样的:

在此处输入图片说明

因此,对于垂直RecyclerView中的每个元素,我需要另一个水平方式。有点像学校的时间表,左边是天,右边是实际时间表,可以水平滚动。

我设法通过将RecyclerView放在第一个RecyclerView项目中来实现这一点。一切正常,但所有水平RecyclerView都在单独滚动。我想要做的是让所有水平RecyclerView同时同步和滚动。我怎样才能做到这一点?

我在垂直适配器的onBindViewHolder方法中设置适配器和水平RecyclerView的方式是这样的:

scheduleAdapter = new ScheduleAdapter(context, data);
holder.scheduleRecyclerView.setAdapter(scheduleAdapter);
holder.scheduleRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
Run Code Online (Sandbox Code Playgroud)

android synchronization horizontal-scrolling android-recyclerview

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

从 Android Studio UI 运行时,Kotlin 单元测试始终通过

我正在尝试在我的应用程序中使用 Kotlin 实现一些单元测试。出现的问题是,当我从 UI 运行测试时,无论我做什么,测试总是通过。如果我从终端运行测试,一切都会按预期进行,如果给出不正确的值,测试就会失败。有谁知道为什么会这样?这是Android Studio的问题吗?有办法解决吗?从 UI 运行时,即使此测试通过

@Test
fun testMe() {
    assertEquals(true, false)
}
Run Code Online (Sandbox Code Playgroud)

android unit-testing kotlin android-studio

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

动态改变渐变中心

我有以下渐变

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:angle="90"
    android:centerColor="#42f448"
    android:centerX="0.5"
    android:centerY="0.5"
    android:endColor="#000000"
    android:startColor="#000000"
    android:type="linear" />
Run Code Online (Sandbox Code Playgroud)

我需要做的是基于ListView上的位置更改来更改渐变的中心。是否可以通过编程方式创建此渐变?

我已经尝试过这种方法,但是如果我更改中心值,则什么也没有发生,它将保持不变:

int colors[] = {0xff000000, 0xff42f448, 0xff000000};
    GradientDrawable shape = new GradientDrawable();
    shape.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
    shape.setColors(colors);
    shape.setGradientCenter(0.3f, 0.3f);
    view.setBackgroundDrawable(shape);
Run Code Online (Sandbox Code Playgroud)

我想念什么?谢谢。

android gradient programmatically

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

验证附加了哪个活动片段

有没有办法验证附加了哪个活动片段?

我尝试过类似的东西:

if (getActivity().equals(MainActivity.class))
Run Code Online (Sandbox Code Playgroud)

但没有结果.

android android-fragments

4
推荐指数
1
解决办法
957
查看次数

从Android应用程序打开文件管理器

如何重定向我的应用程序以在特定路径打开文件管理器?

我尝试过类似的东西:

Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("*/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM,
            Uri.fromFile(new File(filePath)));
    shareIntent.setPackage("my.package");
    startActivity(shareIntent);
Run Code Online (Sandbox Code Playgroud)

但我一直得到错误:

E/AndroidRuntime(3591):android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.SEND typ = / flg = 0x1 pkg = my.package(has clip)(有附加内容)}

什么是正确的意图过滤器,因为我怀疑ACTION_SEND不正确.

谢谢.

android file-manager android-intent

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

如何输入Android EditText时如何调用函数

这是我的情景.我想将EditText集成到搜索功能中.但我不希望它是一个外部按钮,我想在EditBox中键入该函数.

我怎样才能做到这一点?谢谢.

java android android-edittext

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

ImageView不显示某些图像

我正在尝试在AndroidImageView中显示图像,但由于未知原因,某些图像不会显示.工作的图像相对较小,不工作的图像较大(1280x720)左右.我甚至试图通过在.xml文件中添加maxWidth或maxHeight甚至是代码来限制图像的大小,但仍然没有结果.

这是我的ImageView的.xml代码;

<ImageView
            android:id="@+id/imageImageView"
            android:layout_width="fill_parent"
            android:maxHeight="100dp"
            android:maxWidth="100dp"
            android:layout_height="wrap_content"
            android:contentDescription="@string/empty"
            android:gravity="center"
            android:src="@drawable/test3" />
Run Code Online (Sandbox Code Playgroud)

而对于我的代码:

ImageView imageView = (ImageView) view
                .findViewById(R.id.imageImageView);
        imageView.requestLayout();
        imageView.getLayoutParams().height = 100;
        imageView.getLayoutParams().width = 100;
Run Code Online (Sandbox Code Playgroud)

有什么想法/建议吗?谢谢.

android android-imageview

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