RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-192954

Heaven's questions

Martin Hope
Heaven
Asked: 2020-09-14 16:40:45 +0000 UTC

传感器事件监听器

  • 0

我在其中实现了接口SensorEventListener,MainActivity实际上是它的方法:

@Override
public void onSensorChanged(SensorEvent event) {

}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

在OnCreate添加

mSensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    Sensor accelerometer = mSensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (accelerometer != null) {
        mSensorMgr.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

当“摇晃”手机时,会出现一条弹出消息,虽然它没有在任何地方指出......它来自哪里以及如何删除它?

吐司

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-08-21 16:16:59 +0000 UTC

GSON转换器安卓

  • 0

有一个来自服务器的 JSON 字符串,它包含可以处于不同状态的字段,例如,来自服务器的第一个请求可以返回如下 JSON:

"user":{"is_new":false,"user_id":390,"need_fields":["name","last_name","email"]}

当再次被问到:

"user":{"is_new":false,"user_id":390,"need_fields":"false"}

告诉我如何将来自服务器的此类响应转换为UserObject

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-06-21 16:19:56 +0000 UTC

ImageMagnifier 如何添加边框

  • 0

有ImageMagnifier,它是ImageView的“放大镜”:

public class ImageMagnifier extends android.support.v7.widget.AppCompatImageView {

    private PointF zoomPos;
    private boolean zooming = false;
    private Matrix matrix;
    private Paint paint;
    private Bitmap bitmap;
    private BitmapShader shader;
    private int sizeOfMagnifier = 200;

    public ImageMagnifier(Context context) {
        super(context);
        init();
    }

    public ImageMagnifier(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public ImageMagnifier(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        zoomPos = new PointF(0, 0);
        matrix = new Matrix();
        paint = new Paint();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();

        zoomPos.x = event.getX();
        zoomPos.y = event.getY();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                zooming = true;
                this.invalidate();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                zooming = false;
                this.invalidate();
                break;

            default:
                break;
        }

        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!zooming) {
            buildDrawingCache();
        } else {

            bitmap = getDrawingCache();
            shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

            paint = new Paint();
            paint.setShader(shader);

            matrix.reset();
            matrix.postScale(2f, 2f, zoomPos.x, zoomPos.y);
            paint.getShader().setLocalMatrix(matrix);
            canvas.drawCircle(zoomPos.x, zoomPos.y - 150, sizeOfMagnifier, paint);
        }
    }
}

一切正常,但我不知道如何为放大镜添加框架。试图做类似的事情:

paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);

但在这种情况下,得到了一个反转:放大的图像显示在一个框架中(应该是黑色的),放大镜本身是空的。

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-04-20 23:39:51 +0000 UTC

新邮件(Android API)

  • 3

无法访问API新邮件。

我用Retrofit它来联网。

这就是我创建界面的方式:

@FormUrlEncoded
@POST("/v2.0/json")
Call<NovaPochtaSityModel> getSity(@Field("apiKey") String key, @Field("modelName") String modelName, @Field("calledMethod") String calledMethod);

好吧,那我转向服务器:

Call<NovaPochtaSityModel> call = api.getSity("key","InternetDocument","getDocumentList");
    call.enqueue(new Callback<NovaPochtaSityModel>() {
        @Override
        public void onResponse(Call<NovaPochtaSityModel> call, Response<NovaPochtaSityModel> response) {
            Log.d("TAG", "Success");
        }

        @Override
        public void onFailure(Call<NovaPochtaSityModel> call, Throwable t) {
            Log.d("TAG", "Fail");
        }
    });

好了,实际的初始化:

Gson gson = new GsonBuilder().create();
    Retrofit retrofit = new Retrofit.Builder()
            .client(getUnsafeOkHttpClient())
            .baseUrl("https://api.novaposhta.ua")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    API api = retrofit.create(API.class);

作为回应,我发现:

java.net.SocketTimeoutException: failed to connect to api.novaposhta.ua/185.128.233.69 (port 8883) after 10000ms

我将等待时间增加到 60 秒,但没有帮助。是的,并且没有足够的数据来拉动它们超过一分钟。

更新: 在此处输入图像描述

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-03-25 10:48:53 +0000 UTC

TabLayout 问题,onPause() 方法未被调用

  • 0

我有ProductFragment它位于TabLayout和ViewPage

片段内容(fragment_product.xml):

 <android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:ddIndicatorColor="@color/colorArrow"
    app:ddAnimatedIndicator="lineMove"
    app:ddIndicatorHeight="3dp"
    app:tabBackground="@color/colorPrimary"/>

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/tab_layout"
    android:id="@+id/viewPager"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true">
</android.support.v4.view.ViewPager>

在ProductFragment,在方法中OnStart,我将项目(3 个片段)添加到ViewPage

方法本身OnStart

@Override
public void onStart() {
    super.onStart();
    getActivity().setTitle(product.getName());

    aboutTabFragment = new  AboutTabFragment();
    optionsTabFragment = new OptionsTabFragment();
    reviewsTabFragment = new ReviewsTabFragment();

    viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
    viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
    tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);

    viewPagerAdapter.addFragments(aboutTabFragment,getString(R.string.about_product_text));
    viewPagerAdapter.addFragments(optionsTabFragment,getString(R.string.optons_product_text));
    viewPagerAdapter.addFragments(reviewsTabFragment,getString(R.string.reviews_product_text));

    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

}

没有什么特别之处,片段,几个文本字段,仅此而已AboutTabFragment。OptionsTabFragmentReviewsTabFragment

现在的问题是:当在片段ProductFragment中调用片段的生命周期终止方法时onPause-> onStop..etc。until onDetachin nested fragments 甚至没有被调用onPause,因此,当重新打开ProductFragment生命周期开始的方法时,in nested fragments 不会被调用。

PS 我试图onDestroy从父级调用嵌套片段的方法ProductFragment,它仍然没有帮助。

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-03-24 21:14:28 +0000 UTC

使用 HTTPS 改造工作

  • 2

我使用 Retrofit2,当我尝试访问 https 协议时,它开始发誓,我如何才能将证书连接到它?在网上,我找到了类似这样的代码:

OkHttpClient okHttp = new OkHttpClient();
okHttp.setSslSocketFactory(getSSLConfig(contex).getSocketFactory());

Retrofit retrofit = builder.client(okHttp).build();
retrofit.create(serviceClass)

我连接了但是......我没有OkHttp在那里找到方法。setSslSocketFactory

PS 连接 OkHttp 来自:

编译'com.squareup.okhttp3:okhttp:3.6.0'

资料来源:

 Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl("https://site.cf")
            .addConverterFactory(GsonConverterFactory.create(gson));

    CertificatePinner certificatePinner = new CertificatePinner.Builder().add("site.cf","sha256/key").build();
    OkHttpClient client = new OkHttpClient.Builder()
            .certificatePinner(certificatePinner)
            .build();
    Retrofit retrofit = builder.client(client).build();
    API api = retrofit.create(API.class);
android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-22 12:14:59 +0000 UTC

获取文件扩展名

  • 0

我有一个OnClick实现以下功能的方法:

Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Выберите файл для загрузки "), 1);

之后,文件列表打开,选择后我们转到方法OnActivityResult,在那里我们得到Intent在OnClick.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)

如何通过变量获取文件扩展名data?我获得了可以获取文件的完整路径,但名称不正确。例如,在我的手机中媒体文件被调用video0515.mp4,在数据中我得到文件的路径:

Intent { dat=content://com.android.providers.media.documents/document/video:10515 flg=0x1 }

并且无法以任何方式获得扩展名。

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-22 08:29:50 +0000 UTC

重命名文件(安卓)

  • 0

任务看似微不足道,实则难度不小。

我在方法中收到文件的地址,onActivityResult然后创建它

File file = new File(data.getData().getPath());

创建成功,没有问题。然后我尝试像这样重命名文件:

File file = new File(data.getData().getPath());
File file2 = new File(data.getData().getPath(),"324234235461.txt");
file1.renameTo(file2);

las,什么也没有发生。该文件保留在调用构造函数期间收到的名称new File(data.getData().getPath());

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-21 15:38:27 +0000 UTC

Voximplant(呼叫队列)

  • 1

我正在尝试处理 Voximplant,我需要实现从用户到接线员的呼叫,如果接线员正忙,那么如果用户没有中断连接,则用户会停止排队并且不会移除线路。我熟悉了文档,但没有找到任何类似的东西,是否可以在 Voximplant 的帮助下实现它,如果不是......“戳”我需要朝哪个方向移动以实现此调用功能。

PS 电话应该是免费的

javascript
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-14 16:52:32 +0000 UTC

如何检查互联网访问?

  • 7

告诉我如何检查 Internet 连接的可用性?它是可用性,有些情况下 Internet 已打开但不提供流量(例如,帐户余额为零)

还是自己检查一下服务器的连接比较容易,如果上网了,服务器有反应,那说明一切正常,如果上网了,服务器没反应,那就是上不了网了?

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-10 16:12:51 +0000 UTC

去片段时如何隐藏菜单

  • 2

在NavigationDrawer(包括片段的主要活动)我有一个menu

getMenuInflater().inflate(R.menu.navigation_drawer, menu);
MenuInflater inflater = getMenuInflater();

对我来说,当切换到一个片段(主要片段,它一完成就会出现NavigationDrawer)时,打开菜单(在屏幕上用红色圈出)被隐藏,只有在切换到其他片段时才会出现。

在此处输入图像描述

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-07 01:27:54 +0000 UTC

构建应用程序时任务“:app:transformClassesAndResourcesWithProguardForRelease”错误执行失败

  • 3

我正在尝试构建应用程序的发布版本并获取 apk。作为回应,我收到一条消息

 Generate Signed APK
    Errors while building APK. You can find the errors in the 'Messages' view. 

在日志中这样写

错误:任务 ':app:transformClassesAndResourcesWithProguardForRelease' 执行失败。任务失败,查看日志了解详情

好吧,它指的是用于处理网络的库

Warning:library class org.apache.http.impl.client.DefaultHttpClient depends on program class org.apache.http.params.HttpParams
Warning:library class org.apache.http.impl.client.DefaultHttpClient depends on program class org.apache.http.protocol.HttpRequestExecutor
Warning:library class org.apache.http.impl.client.DefaultHttpClient depends on program class org.apache.http.protocol.HttpContext
Warning:library class org.apache.http.impl.client.DefaultHttpClient depends on program class org.apache.http.ConnectionReuseStrategy
Warning:library class org.apache.http.impl.client.DefaultHttpClient depends on program class org.apache.http.protocol.BasicHttpProcessor
Warning:there were 74 unresolved references to classes or interfaces.
Warning:there were 127 instances of library classes depending on program classes.
Warning:Exception while processing task java.io.IOException: Please correct the above warnings first.

事件簿

13:43:52 Gradle build finished with 1 error(s) and 169 warning(s) in 13s 295ms
13:43:52 Build APK: Errors while building APK. You can find the errors in the 'Messages' view.
android
  • 2 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-02-01 16:48:49 +0000 UTC

使用相机(安卓)

  • 1

我打开相机并以这种方式从相机中拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 2);

然后我onActivityResult抓住了结果

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == 2) {         
                Uri uri = data.getData();

问题本身:如果相机在拍照后询问“保存?” 而不是被压data进来null。如果在设备上禁用确认保存照片,则一切正常。

我可以假设 android 不允许onActivityResult访问快照后的照片并阻止它,并且在用户确认保存照片后,onActivityResult它不再捕获答案

java
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-30 22:07:37 +0000 UTC

使用 onLongClick(RecyclerView)

  • 1

有一种方法可以在您长按元素时将其移除:

@Override
    public boolean onLongClick(View v) {
        files.remove(files.get(getAdapterPosition()));
        fAdapter.notifyDataSetChanged();
        return true;
    }

问题是,如何确保不是删除,而是一开始的ActionBar中有确认?要有 2 个按钮“删除”和“取消”?

我是否应该为 ActionBar 编写一个新菜单并将其替换为长按,然后根据按下的按钮返回之前的菜单?

看起来这个实现是某种“曲线”......也许有标准工具可以让你实现这样的功能?

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-29 03:36:49 +0000 UTC

ActionBar(item)占用空间大

  • 1

如何减小尺寸item'а使其尺寸受其内容限制?

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/add_file"
    android:icon="@drawable/ic_plus_1"
    android:title="Add..."
    app:showAsAction="always"/>

<item
    android:icon="@drawable/ic_video2"
    android:title="Photo"
    app:showAsAction="always"
    android:id="@+id/photo_ic"/>

这就是我添加它的方式

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    //super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.add_file, menu);

在此处输入图像描述

由于我添加的菜单,Title 的内容不完全适合 ActionBar

java
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-25 15:50:19 +0000 UTC

适配器 RecyclerView

  • 0

帮助聊天适配器。

Apadter 现在看起来像这样:

public class MSGAdapter extends RecyclerView.Adapter<MSGAdapter.MSGViewHolder> {

ArrayList<MessageModel> messages;

public MSGAdapter(ArrayList<MessageModel> messages) {
    this.messages = messages;
}

@Override
public MSGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_card_layout, parent, false);
    MSGAdapter.MSGViewHolder nh = new MSGAdapter.MSGViewHolder(v);
    return nh;
}

@Override
public void onBindViewHolder(MSGViewHolder holder, int position) {
       Context ctx = holder.itemView.getContext();
       LinearLayout.LayoutParams lparamsFile = new LinearLayout.LayoutParams(150, 150);
        holder.message_.setText(messages.get(position).getContent().toString()); // Добавление текста сообщения
        if(messages.get(position).getFile() != null) {
            for (int i = 0; i < messages.get(position).getFile().size(); i++) {
                ImageView file = new ImageView(ctx);//Изображение вложения
                file.setScaleType(ImageView.ScaleType.CENTER_CROP);
                file.setLayoutParams(lparamsFile);
                switch (messages.get(position).getFile().get(i).getType().toString()) {
                    case "img":
                        Glide.with(ctx).load(messages.get(position).getFile().get(i).getSrc()).into(file);
                        break;
                    case "doc":
                        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.doc));
                        break;
                    case "audio":
                        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.audio));
                        break;
                    case "video":
                        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.video));
                        break;
                }

                holder.fileLayout.addView(file);
            }
        }
}


@Override
public int getItemCount() {
    return messages.size();
}

public static class MSGViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    CardView cv;
    LinearLayout mainLayout, messageLayout, fileLayout;
    TextView message_;
    public MSGViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.msg_card);

        mainLayout = (LinearLayout) itemView.findViewById(R.id.main_layout);
        messageLayout = (LinearLayout) itemView.findViewById(R.id.message_layout);
        fileLayout = (LinearLayout) itemView.findViewById(R.id.file_layout);
        message_ = (TextView) itemView.findViewById(R.id.message);
    }

    @Override
    public void onClick(View view) {
    }
}

消息(和附件)正常显示,只有在滚动消息列表时,附件才会“混乱”。

我已经明白这是因为我在 中声明了消息附件 ( ImageView ) OnBindViewHolder,如果我事先不知道消息中会有多少附件,我如何将它“传输”到 OnCreateViewHolder 阶段?messages.get(position).getFile()我需要在发送消息时从数组 ( ) 中获取它们(附件)

android
  • 3 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-24 21:30:40 +0000 UTC

RecyclerView(洗牌所有项目)

  • 0

我有一个适配器,它用 3 种布局 (LinerLayout) 填充CardView,而它们又包含一个TextView和一个 ImageView。当填充列表(当它离开屏幕时)并翻转它时,上面的元素混合在一起,几个TextViews可以进入一个LinearLayout。

public class MSGAdapter extends RecyclerView.Adapter<MSGAdapter.MSGViewHolder> {

ArrayList<MessageModel> messages;

public MSGAdapter(ArrayList<MessageModel> messages) {
    this.messages = messages;
}

@Override
public MSGViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_card_layout, parent, false);
    MSGAdapter.MSGViewHolder nh = new MSGAdapter.MSGViewHolder(v);
    return nh;
}

@Override
public void onBindViewHolder(MSGViewHolder holder, int position) {
    Context ctx = holder.itemView.getContext();

    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams lparamsFile = new LinearLayout.LayoutParams(150, 150);
    LinearLayout.LayoutParams mLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    LinearLayout mLinerLayout = new LinearLayout(ctx); //Основной контейнер
    mLinerLayout.setOrientation(LinearLayout.VERTICAL);
    mLinerLayout.setLayoutParams(mLayout);

    LinearLayout linearLayoutTextContent = new LinearLayout(ctx);     //Контейнер для текста сообщения
    linearLayoutTextContent.setOrientation(LinearLayout.HORIZONTAL);
    linearLayoutTextContent.setLayoutParams(lparams);

    LinearLayout linearLayoutFileContent = new LinearLayout(ctx);   //Контейнер для вложения
    linearLayoutFileContent.setOrientation(LinearLayout.HORIZONTAL);
    linearLayoutFileContent.setLayoutParams(lparams);
    linearLayoutFileContent.setPadding(10,10,10,10);


    TextView messageContent = new TextView(ctx);    //Текст сообщения
    messageContent.setLayoutParams(lparams);
    messageContent.setPadding(20,20,20,20);
    messageContent.setText(messages.get(position).getContent().toString()); // Добавление текста сообщения

    if(messages.get(position).getFile() != null) {
            for (int i = 0; i < messages.get(position).getFile().size(); i++) {
                ImageView file = new ImageView(ctx);//Изображение вложения
                file.setScaleType(ImageView.ScaleType.CENTER_CROP);
                file.setLayoutParams(lparamsFile);
                switch (messages.get(position).getFile().get(i).getType().toString()) {
                    case "img":
                        Glide.with(ctx).load(messages.get(position).getFile().get(i).getSrc()).into(file);
                        break;
                    case "doc":
                        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.doc));
                        break;
                    case "audio":
                        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.audio));
                        break;
                    case "video":
                        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.video));
                        break;
                }

                linearLayoutFileContent.addView(file);
            }

    }
    linearLayoutTextContent.addView(messageContent); //Добавление заполненного контейнера в диалог

    if(messages.get(position).getFile() != null && !(messages.get(position).getContent().equals("null"))) {  // Сообщение и вложение
        mLinerLayout.addView(linearLayoutTextContent);
        mLinerLayout.addView(linearLayoutFileContent);
        holder.cv.addView(mLinerLayout);
    } else if(messages.get(position).getFile() != null && messages.get(position).getContent().equals("null")){ // Вложение
        mLinerLayout.addView(linearLayoutFileContent);
        holder.cv.addView(mLinerLayout);
    } else if(messages.get(position).getFile() == null && !(messages.get(position).getContent().equals("null"))) { // Сообщение
        mLinerLayout.addView(linearLayoutTextContent);
        holder.cv.addView(mLinerLayout);

    }

}

@Override
public int getItemCount() {
    return messages.size();
}

public static class MSGViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    CardView cv;

    public MSGViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.msg_card);

    }

    @Override
    public void onClick(View view) {
    }
}
android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-24 01:16:54 +0000 UTC

布局的编程定位

  • 0

我动态创建了 2 个布局(它们在 CardView 中)

LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    //LinearLayout.LayoutParams lparamsFile = new LinearLayout.LayoutParams(150, 150);

    LinearLayout linearLayoutTextContent = new LinearLayout(ctx);     //Контейнер для текста сообщения
    linearLayoutTextContent.setOrientation(LinearLayout.HORIZONTAL);
    linearLayoutTextContent.setLayoutParams(lparams);

    LinearLayout linearLayoutFileContent = new LinearLayout(ctx);//Контейнер для вложения
    linearLayoutFileContent.setOrientation(LinearLayout.HORIZONTAL);
    linearLayoutFileContent.setLayoutParams(lparams);

我怎样才能让我linearLayoutFileContent在linearLayoutTextContent

android
  • 1 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-23 17:27:11 +0000 UTC

填充 RecyclerView

  • 0

由于某些未知原因,我无法向 RecyclerView 添加元素。

我有一个方法onActivityResult,我在其中做了以下事情:

file.add(new FileModel());

反过来,在 OnStart 方法中更高的地方:

file = new ArrayList<>();

fAdapter = new ADDFileAdapter(file);
rvAddFile.setAdapter(fAdapter);

如果我填写file该方法OnStart,则会添加并显示元素。

        rvAddFile.setAdapter(fAdapter);
        file.add(new FileModel());
        file.add(new FileModel());
        file.add(new FileModel());

问题是为什么在onActivityResult方法中没有将元素添加到RecyclerView中?

更新程序

public class ADDFileAdapter extends RecyclerView.Adapter<ADDFileAdapter.ADDFileViewHolder> {

ArrayList<FileModel> fileConteiner;

public ADDFileAdapter(ArrayList<FileModel> fileConteiner) {
    this.fileConteiner = fileConteiner;
}

@Override
public ADDFileViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_file_card_layout, parent, false);
    ADDFileAdapter.ADDFileViewHolder nh = new ADDFileAdapter.ADDFileViewHolder(v);
    return nh;
}

@Override
public void onBindViewHolder(ADDFileViewHolder holder, int position) {
    Context ctx = holder.itemView.getContext();
    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams lparamsFile = new LinearLayout.LayoutParams(100,100);

    LinearLayout linearLayoutFileContent = new LinearLayout(ctx);     //Контейнер для вложения 
    linearLayoutFileContent.setLayoutParams(lparams);
    for (int i = 0; i < fileConteiner.size(); i++){
        ImageView file = new ImageView(ctx);    //Изображение вложения
        file.setLayoutParams(lparamsFile);
        file.setImageDrawable(ctx.getResources().getDrawable(R.drawable.audio));
        linearLayoutFileContent.addView(file);
    }
    holder.cv.addView(linearLayoutFileContent);
}

@Override
public int getItemCount() {
    return fileConteiner.size();
}

public static class ADDFileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    CardView cv;

    public ADDFileViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.add_file_card);
    }

    @Override
    public void onClick(View view) {
    }
}

方法onActivityResult

if(u.toString().equals("image/jpeg")
                     || u.toString().equals("image/png")
                     || u.toString().equals("image/gif")
                     || u.toString().equals("image/tiff")){

                 fileAdd.add(new FileModel());
                 fAdapter.notifyDataSetChanged();
             }

调试器

在此处输入图像描述

android
  • 2 个回答
  • 10 Views
Martin Hope
Heaven
Asked: 2020-01-20 17:01:05 +0000 UTC

从存储中下载文件

  • 1

尝试从存储中将 .img 文件动态添加到 ImageView

这里我撕下activity选择一张图片,现阶段没有问题

item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Выберите файл для загрузки "), 1);
            return false;
        }
    });
}

然后我尝试获取所选图像的路径

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            PATH = data.getData().getPath();
            ImageView img = new ImageView(getContext());
            LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            img.setLayoutParams(lparams);
            //img.setImageURI(Uri.fromFile(imgFile));
            Picasso.with(getContext()).load(PATH).resize(50, 50).into(img);
            mainLayout.addView(img);
        }
    }

}

值“/document/image:1552”来到 PATH 变量,在这个阶段似乎一切都很好,但只有当它被添加到 mainLayout 时......唉,什么都没有添加

PS 我尝试通过服务器的 URL 下载它,一切都已成功加载,显然在获取文件路径时有一个门框。

android
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5