在 Main Activity 上调用时,Fragment Recyclerview 不会填充远程数据

Fragment Recycler View doesn't get Populated with remote ddata when called on Main Activity

我不确定我现在遗漏了什么,当我尝试在要显示列表的视图上拖动时,我有点显示存在回收器视图,但上面没有数据。我没有收到任何错误,但视图中没有填充远程数据。我有一个主 activity 托管一个片段,其内容是根据 json

的响应动态生成的

这是我的片段:RCurrency.java

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class RCurrency extends Fragment {
    RecyclerView mRecyclerView;
    List<CurrencyItem> mCurrencyItems;
    String currencies = "Currency";
    Context mContext;
    public static final String URL_STRING = "https://linktoapi.com/";

    public RCurrency(){
        //required empty constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView( LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        //fragment inflater
        View view = inflater.inflate(R.layout.fragment_currency_home,container, false);

        //recycler view
        mRecyclerView = (RecyclerView) view.findViewById(R.id.currency_recycler_view);

        mCurrencyItems = new ArrayList<CurrencyItem>();

        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_STRING,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            JSONObject jsonObject;
                            for (int i = 0; i <= jsonArray.length()-1; i++) {
                                jsonObject = jsonArray.getJSONObject(i);

                                CurrencyItem currencyItem = new CurrencyItem();

                                currencyItem.setCurrencyLogo(jsonObject.getString("id"));
                                currencyItem.setSymbol(jsonObject.getString("symbol"));
                                currencyItem.setCurrencyName(jsonObject.getString("name"));
                                mCurrencyItems.add(currencyItem);
                            }
                            CurrencyItemAdapter adapter = new CurrencyItemAdapter(getContext(), mCurrencyItems);

//Problem at this point
                            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
                            mRecyclerView.setLayoutManager(layoutManager);
                            mRecyclerView.setHasFixedSize(true);
                            mRecyclerView.setAdapter(adapter);
                            adapter.notifyDataSetChanged();

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Check response", error);
                    }
                }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("name",currencies);

                return params;
            }

            //add request to the volley queue

        };
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        requestQueue.add(stringRequest);
        return view;
    }
}

我主要 Activity 在这里托管片段:MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button logInBtn;

    private CurrencyListFragment RCurrency;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        fragment manager to manage fragments and transactions

        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
        if (fragment == null){
            fragment = new RCurrency();

            fragmentManager.beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit();
        }

        logInBtn = (Button) findViewById(R.id.log_in_main_page);

        logInBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, info.lansachia.cryptoinvest.LoginActivity.class);
                finish();
                startActivity(intent);
            }
        });
    }


}

我的适配器:CurrencyItemAdapter.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;



public class CurrencyItemAdapter extends RecyclerView.Adapter<CurrencyItemAdapter.MyViewHolder> {
    private List<CurrencyItem> mCurrencyItems;
    private Context mContext;

    public CurrencyItemAdapter(Context context, List<CurrencyItem> currencyItems){
        this.mContext = context;
        this.mCurrencyItems = currencyItems;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_currency, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        CurrencyItem currencyItem = mCurrencyItems.get(position);

        //setting text to currency symbol
        holder.mSymbol.setText(currencyItem.getSymbol());

        holder.mSymbol.setTextSize(17);

        //currency name
        holder.mName.setText(currencyItem.getCurrencyName());
        holder.mName.setTextSize(17);

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView mSymbol;
        TextView mPrice;
        TextView mName;
        TextView mRecommendation;
        ImageView mCurrencyLogo;


        public MyViewHolder(View itemView) {
            super(itemView);

            mSymbol = (TextView)itemView.findViewById(R.id.currency_symbol);
            mPrice = (TextView)itemView.findViewById(R.id.currency_price);
            mName = (TextView)itemView.findViewById(R.id.currency_name);
            mRecommendation = (TextView)itemView.findViewById(R.id.currency_recommendation);
//            mCurrencyLogo= (ImageView)itemView.findViewById(R.id.currency_icon);
        }

    }


}

list_item_currency.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/currency_icon"
        android:layout_width="75dp"
        android:layout_height="74dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/currecy_logo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/currency_symbol"
        android:layout_width="177dp"
        android:layout_height="75dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toEndOf="@+id/currency_icon"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/currency_name"
        android:layout_width="176dp"
        android:layout_height="29dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="91dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/currency_symbol"
        app:layout_constraintVertical_bias="0.0"/>


    <TextView
        android:id="@+id/currency_recommendation"
        android:layout_width="101dp"
        android:layout_height="25dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/currency_name"
        app:layout_constraintTop_toBottomOf="@+id/currency_price"/>

    <TextView
        android:id="@+id/currency_price"
        android:layout_width="101dp"
        android:layout_height="74dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/currency_symbol"
        app:layout_constraintTop_toTopOf="parent"/>


</android.support.constraint.ConstraintLayout>

fragment_currency.xml Xml 使用 Recycler View 但最终回收器视图托管在 main_activity.xml

的布局中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:id="@+id/fragment_home_page"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
                                            android:id="@+id/currency_recycler_view"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:layout_marginBottom="8dp"
                                            android:layout_marginEnd="8dp"
                                            android:layout_marginStart="8dp"
                                            android:layout_marginTop="8dp"
                                            >
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

此处的堆栈跟踪:

3-03 10:07:16.102 5337-5337/? I/zygote: Not late-enabling -Xcheck:jni (already on)
03-03 10:07:16.176 5337-5337/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
03-03 10:07:16.387 5337-5337/info.myapp.currency W/ActivityThread: Application info.myapp.currency is waiting for the debugger on port 8100...
03-03 10:07:16.400 5337-5337/info.myapp.currency I/System.out: Sending WAIT chunk
03-03 10:07:17.018 5337-5346/info.myapp.currency I/zygote: Debugger is active
03-03 10:07:17.032 5337-5337/info.myapp.currency I/System.out: Debugger has connected
03-03 10:07:17.032 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:17.233 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:17.435 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:17.636 5337-5337/info.myapp.currency I/chatty: uid=10085(u0_a85) info.myapp.currency identical 1 line
03-03 10:07:17.838 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.039 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.241 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.443 5337-5337/info.myapp.currency I/System.out: waiting for debugger to settle...
03-03 10:07:18.645 5337-5337/info.myapp.currency I/System.out: debugger has settled (1460)
03-03 10:07:19.002 5337-5337/info.myapp.currency W/zygote: Skipping duplicate class check due to unrecognized classloader
03-03 10:07:19.008 5337-5337/info.myapp.currency W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
03-03 10:07:19.013 5337-5337/info.myapp.currency W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
03-03 10:07:19.058 5337-5337/info.myapp.currency I/BiChannelGoogleApi: [FirebaseAuth: ] No Fallback module; NOT setting up for lazy initialization
03-03 10:07:19.095 5337-5337/info.myapp.currency D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
03-03 10:07:19.125 5337-5359/info.myapp.currency W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
03-03 10:07:19.156 5337-5359/info.myapp.currency I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.
03-03 10:07:19.156 5337-5359/info.myapp.currency I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
03-03 10:07:19.249 5337-5337/info.myapp.currency I/FirebaseInitProvider: FirebaseApp initialization successful
03-03 10:07:19.252 5337-5337/info.myapp.currency I/InstantRun: starting instant run server: is main process
03-03 10:07:19.293 5337-5362/info.myapp.currency I/FA: App measurement is starting up, version: 11910
03-03 10:07:19.293 5337-5362/info.myapp.currency I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
03-03 10:07:19.293 5337-5362/info.myapp.currency I/FA: To enable faster debug mode event logging run:
                                                                 adb shell setprop debug.firebase.analytics.app info.myapp.currency
03-03 10:07:19.293 5337-5362/info.myapp.currency D/FA: Debug-level message logging enabled
03-03 10:07:19.611 5337-5337/info.myapp.currency D/Currency List: Fragment added
03-03 10:07:19.630 5337-5337/info.myapp.currency D/RCurrency: onCreate Called
03-03 10:07:19.715 5337-5366/info.myapp.currency D/NetworkSecurityConfig: No Network Security Config specified, using platform default
03-03 10:07:19.777 5337-5337/info.myapp.currency D/RCurrency: onCreateView Called
03-03 10:07:19.805 5337-5373/info.myapp.currency D/OpenGLRenderer: HWUI GL Pipeline
03-03 10:07:19.806 5337-5362/info.myapp.currency D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-8822734687014467414}]
03-03 10:07:19.884 5337-5373/info.myapp.currency I/OpenGLRenderer: Initialized EGL, version 1.4
03-03 10:07:19.884 5337-5373/info.myapp.currency D/OpenGLRenderer: Swap behavior 1
03-03 10:07:19.884 5337-5373/info.myapp.currency W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
03-03 10:07:19.884 5337-5373/info.myapp.currency D/OpenGLRenderer: Swap behavior 0
03-03 10:07:19.913 5337-5373/info.myapp.currency D/EGL_emulation: eglCreateContext: 0x8edb3ec0: maj 2 min 0 rcv 2
03-03 10:07:19.973 5337-5373/info.myapp.currency D/EGL_emulation: eglMakeCurrent: 0x8edb3ec0: ver 2 0 (tinfo 0x9e4d6200)
03-03 10:07:20.128 5337-5373/info.myapp.currency D/EGL_emulation: eglMakeCurrent: 0x8edb3ec0: ver 2 0 (tinfo 0x9e4d6200)
03-03 10:07:20.254 5337-5362/info.myapp.currency D/FA: Connected to remote service
03-03 10:07:20.385 5337-5337/info.myapp.currency D/RCurrency: parseData Called
03-03 10:07:20.385 5337-5337/info.myapp.currency D/RCurrency: requestData Called
03-03 10:07:20.437 5337-5344/info.myapp.currency I/zygote: Do partial code cache collection, code=30KB, data=21KB
03-03 10:07:20.440 5337-5344/info.myapp.currency I/zygote: After code cache collection, code=30KB, data=21KB
03-03 10:07:20.440 5337-5344/info.myapp.currency I/zygote: Increasing code cache capacity to 128KB
03-03 10:07:46.609 5337-5346/? W/zygote: Debugger told VM to exit with status -1

                                         [ 03-03 10:07:46.610  5337: 5346 D/         ]
                                         HostConnection::get() New Host Connection established 0xa09e4b00, tid 5346

您是否尝试过先设置适配器,然后再设置布局管理器?

                            mRecyclerView.setAdapter(adapter);
 RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
                            mRecyclerView.setLayoutManager(layoutManager);
                            mRecyclerView.setHasFixedSize(true);

                            adapter.notifyDataSetChanged();

此外,您可以在 XML 中为 RecyclerView 设置它:

<... RecyclerView
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
</RecyclerView

不完全确定这是问题所在,但它可能需要在初始创建时使用布局管理器和适配器。我会尝试设置这些最初,然后一旦你的数据加载你应该能够做到:

CurrencyItemAdapter adapter = new CurrencyItemAdapter(getContext(), mCurrencyItems);
mRecyclerView.swapAdapter(adapter, true);

你像下面代码中使用的那样更改回收站视图布局集..

  mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

如果以上代码有效,请接受答案。

要回答您的问题,请使用 kCIInputIntensityKey 这里: currentImageFilters.setValue(filterIntensity.value, forKey: kCIInputImageKey)

那是因为 CISepiaTone 过滤器需要两个参数: 1- inputImage:显示名称为Image的CIImage对象。

2- inputIntensity:一个NSNumber对象,属性类型为CIAttributeTypeScalar,显示名称为Intensity。