小编Gun*_*lan的帖子

RecyclerView中的单一选择

我知道在recyclerview类中没有默认的选择方法,但是我试过以下方式,

public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.mTextView.setText(fonts.get(position).getName());
    holder.checkBox.setChecked(fonts.get(position).isSelected());

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                for (int i = 0; i < fonts.size(); i++) {
                    fonts.get(i).setSelected(false);
                }
                fonts.get(position).setSelected(isChecked);
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

尝试这段代码时,我得到了预期的输出,但完全没有.

我将用图像解释这个.

默认情况下,从我的适配器中选择第一个项目

在此输入图像描述

然后我试图选择第二个然后是第三个,然后是第四个,最后是第五个,

在此输入图像描述

这里仅应选择第5个,但所有5个都被选中.

如果我将列表滚动到底部并再次返回顶部,

在此输入图像描述

我得到了我的预期,

我怎样才能克服这个问题?还有一段时间如果我快速滚动列表,其他一些项目就会被选中.如何克服这个问题呢?

更新

notifyDataSetChanged()fonts.get(position).setSelected(isChecked);我得到以下异常后,我正在尝试使用

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
        at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:1462)
        at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:2982)
        at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:7493)
        at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:4338)
        at com.app.myapp.screens.RecycleAdapter.onRowSelect(RecycleAdapter.java:111)
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview

60
推荐指数
7
解决办法
8万
查看次数

如何隐藏MPAndroidChart中的图例和轴?

他们是否有可能隐藏这张照片中的所有圆形物品.

在此输入图像描述

我使用了以下代码,

public void setDataList(List<HorizontalBarChartData> dataList, Resources resources) {

    ArrayList<String> categories = new ArrayList<String>();
    ArrayList<BarEntry> values = new ArrayList<BarEntry>();
    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    BarDataSet set1;
    for (int i = 0; i < dataList.size(); i++) {
        categories.add(dataList.get(i).getName());
        values.add(new BarEntry(dataList.get(i).getValue(), i));
    }

    /*set1 = new BarDataSet(values, "Income, Expense, Disposable Income");*/
    set1 = new BarDataSet(values, "Category 1, Category 2, Category 3");
    set1.setBarSpacePercent(35f);
    set1.setColors(new int[]{resources.getColor(R.color.cyan_blue), resources.getColor(R.color.vermilion_tint), resources.getColor(R.color.sea_green)});
    dataSets.add(set1);

    BarData data = new BarData(categories, dataSets);
    data.setValueTextSize(10f);

    horizontalBarChart.setData(data);
}
Run Code Online (Sandbox Code Playgroud)

更新

如何隐藏此图像中的圆角部分?

在此输入图像描述

android mpandroidchart

50
推荐指数
4
解决办法
4万
查看次数

使用GSON创建JSON字符串

我正在上课,

public class Student {
    public int id;
    public String name;
    public int age;    
}
Run Code Online (Sandbox Code Playgroud)

现在我要创建新学生,

//while create new student
Student stu = new Student();
stu.age = 25;
stu.name = "Guna";
System.out.println(new Gson().toJson(stu));
Run Code Online (Sandbox Code Playgroud)

这给了我以下输出,

{"id":0,"name":"Guna","age":25} //Here I want string without id, So this is wrong
Run Code Online (Sandbox Code Playgroud)

所以我在这里想要String

{"name":"Guna","age":25}
Run Code Online (Sandbox Code Playgroud)

如果我想编辑旧学生

//While edit old student
Student stu2 = new Student();
stu2.id = 1002;
stu2.age = 25;
stu2.name = "Guna";
System.out.println(new Gson().toJson(stu2));
Run Code Online (Sandbox Code Playgroud)

现在输出是

{"id":1002,"name":"Guna","age":25} //Here I want the String with Id, So this is …
Run Code Online (Sandbox Code Playgroud)

java android json gson

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

片段没有创建视图

MyFragment.java

public class MyFragment extends Fragment {
    private onItemSelectedListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container,
                false);
        btn_myButton = (Button) view.findViewById(R.id.btn_new_client);
        btn_myButton .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                updateDetail("New Layout");
            }
        });
        return view;
    }

    Button btn_myButton;

    public interface onItemSelectedListener {
        public void onItemSelected(String link);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof onItemSelectedListener) {
            listener = (onItemSelectedListener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + …
Run Code Online (Sandbox Code Playgroud)

android

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

LocationManager.PROVIDERS_CHANGED_ACTION不适用于API 26及更高版本

我正在使用以下代码来获取位置开/关事件.

<receiver
    android:name=".receivers.GpsReceiver"
    android:enabled="true">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

我正在开发基于地理围栏的应用程序.基于仅在需要时重新注册地理围栏,我们必须在应用程序收到GEOFENCE_NOT_AVAILABLE警报后重新注册地理围栏.这通常发生在禁用NLP(Android的网络位置提供程序)之后.

通过使用此广播接收器,我在启用Android的网络位置提供程序时重新注册了地理围栏.

但是从API级别26开始,这个广播接收器永远不会工作.请参阅 后台执行限制.

那么如何在API 26及更高版本中实现相同的任务呢?

注意:即使app在后台,我也需要重新注册地理围栏.

android broadcastreceiver

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

使用堆栈交换API

我试图用堆栈交换api upvote stackoverflow问题,但失败了.我已经尝试了很多,但我没有让它工作.

网址:

http://api.stackexchange.com/2.2/questions/35007869/upvote

文件

https://api.stackexchange.com/docs/upvote-question

Json数据:

{
  "key" : "my key",
  "access_token" : "my token",
  "site" : "stackoverflow.com",
  "preview" : "false",
  "filter": "default"
}
Run Code Online (Sandbox Code Playgroud)

我通过以下参数尝试了fiddler.

User-Agent: Fiddler
Host: api.stackexchange.com
Content-Length: 159
Content-Type: application/json; charset=utf-8
Run Code Online (Sandbox Code Playgroud)

POST方法.但我失败了以下错误消息.

error_id=400
error_message=site is required
error_name=bad_parameter
Run Code Online (Sandbox Code Playgroud)

但我已经在我的JSON对象中提供了该站点.所以任何帮助都会非常值得一提.

更新

虽然在小提琴手中尝试这个,但我得到了以下信息.

在此输入图像描述

php java android json stackexchange-api

10
推荐指数
1
解决办法
618
查看次数

Android WebView 从内部存储加载 | 安卓 11 | 安卓 R

实际上我的要求是我想像图像一样将动态文件加载到网页中。视频、音频等,它来自资产或应用程序自己的文件目录,这不是问题。

我尝试使用以下两种方式。

方式一

这是我的 html 文件在资产文件夹中,我123.jpg在资产文件夹和内部文件夹中都有,

主页.html,

<!DOCTYPE html>
<html>
   <head>
      <title>Page Title</title>
   </head>
   <body>
      <h1>Image testing</h1>
      <p>Relative Path</p>
      <img src="123.jpg" alt="Italian Trulli" width="100%">
      <p>From Files Directory</p>
      <img src="file:////data/user/0/com.guna.testapplication/files/123.jpg" alt="Italian Trulli" width="100%">
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我正在将它加载到 webview 中

webView.loadUrl("file:///android_asset/Home.html")
Run Code Online (Sandbox Code Playgroud)

这是我 29 的输出,

在此处输入图片说明

看,从按预期加载的 Asset 和 files 目录。

这是 30,

在此处输入图片说明

这里仅从资产目录加载。和文件目录未加载。

方式二

在这里,我从内部存储加载 html 和图像。

主页.html

<!DOCTYPE html>
<html>
   <head>
      <title>Page Title</title>
   </head>
   <body>
      <h1>Image testing</h1>
      <img src="123.jpg" alt="Italian Trulli" width="100%">
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我正在像这样加载 webview,

webView.loadUrl("file:////data/user/0/com.guna.testapplication/files/Home.html")
Run Code Online (Sandbox Code Playgroud)

如果我的compileSdkVersion 29和 …

android

8
推荐指数
1
解决办法
1763
查看次数

DialogFragment 中的 Recyclerview

我正在使用以下代码来创建 xml 布局资源。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="@dimen/dp48"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center"
        android:fontFamily="@font/trebuchet"
        android:text="@string/select_a_folder"
        android:textColor="?attr/colorAccent"
        android:textSize="16sp"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintBottom_toTopOf="@+id/recyclerViewFolderList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewFolderList"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="350dp"
        android:maxHeight="350dp"
        app:layout_constraintBottom_toTopOf="@+id/imgViewClose2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/imgViewClose2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/close"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:src="@drawable/ic_baseline_close_24dx"
        android:text="@string/cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recyclerViewFolderList"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

当我加载 recyclerview 中的一些项目时,我得到了预期的输出,

在此输入图像描述

但是,如果我加载近 50 个项目,recyclerview 会填充整个视图,因此我的标题和取消按钮丢失。

在此输入图像描述

如果我设置了layout_height="0dp"recyclerview,recyclerview就完全消失了,如下所示。

在此输入图像描述

我不想修复recyclerview的大小;如果我修复它,它会起作用,但它总是固定的,即使我只传递 2 个项目,那里也会有空白空间。

所以我希望recyclerview应该是wrap_content并且标题和按钮应该是可见的。

android android-recyclerview

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

如何获取分组的Kendo网格中所选行的索引和数据

我正在尝试访问行索引,如下所示:

var grid = $("#grid").data("kendoGrid");
alert(grid.select().index());
Run Code Online (Sandbox Code Playgroud)

我在这个jsfiddle链接中添加了我的代码.这个代码在我的系统中工作,我不知道为什么 deleteRecord()没有在jsfiddle中调用方法,但这不是实际的问题.

在这里点击最后一行的取消按钮提醒消息时会将索引显示为8,但实际索引为4.每个按钮只给我错误的索引.

javascript jquery html5 kendo-ui kendo-grid

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

Camera2Basic app前置摄像头拍摄照片

我试过谷歌的android-Camera2Basic示例应用程序.对于前置摄像头,我在Camera2BasicFragment中更改了一些代码.变化如下.

要切换到的前置摄像头我已经改变mCameraId = cameraId;mCameraId = "1";setUpCameraOutputs(int width, int height)方法.

并添加如果条件在结束时setUpCameraOutputs(int width, int height),

if(mCameraId == null)
    mCameraId = cameraId;
Run Code Online (Sandbox Code Playgroud)

此更改完美显示前置摄像头,但不选择照片.所以我换了

mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                        mBackgroundHandler);
Run Code Online (Sandbox Code Playgroud)

captureStillPicture();
Run Code Online (Sandbox Code Playgroud)

lockFocus().现在前置摄像头拍摄照片,但它看起来颠倒了.

例如:

样本图像

我现在不知道该怎么办.

我的要求是使用camera2 api在两个摄像头中捕捉照片.所以,如果我做错了什么,请纠正我.

在某些设备中,此应用程序只需单击即可拍摄多张照片.

camera android android-camera2

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