无法让SwipeLayout侦听器触发

时间:2017-03-21 07:55:58

标签: android android-layout

我似乎无法触发Swipe布局的监听器。我已经查看了示例和其他问题,但似乎没有任何工作。我真的很想知道我做错了什么。

这是我的活动。主要是:

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/swipe">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#ecf0f1"
        android:elevation="50dp"

        tools:context="com.example.napstar.movieapp2.MainActivity">


        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:id="@+id/movies_list"
            android:divider="@null"
            android:dividerHeight="0dp"

            >


        </ListView>
    </LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>

以下是活动的oncreate方法:

    ArrayList<MovieModel> moviesList;
    Context context;
    private Typeface tfMedium,tfLight;

    private static final String DEBUG_TAG = "MainActivity";

    private ProgressDialog loading = null;
    private SwipeRefreshLayout swipeContainer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

            boolean refrshToggle=true;

        moviesList= new ArrayList<MovieModel>();
        context=getApplicationContext();
        this.setFinishOnTouchOutside(true);

        try
        {
            swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipe);

            swipeContainer.setDistanceToTriggerSync(20);
            swipeContainer.setSize(SwipeRefreshLayout.DEFAULT);
            swipeContainer.setProgressViewOffset(false, 0,100);
            // Configure the refreshing colors
            swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
                    android.R.color.background_dark,
                    android.R.color.holo_orange_light,
                    android.R.color.holo_red_light);

 // Check if the NetworkConnection is active and connected.
            ConnectivityManager connMgr = (ConnectivityManager)
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            boolean isConnected = networkInfo != null &&
                    networkInfo.isConnectedOrConnecting();
            if (isConnected) {

                new MoviesAsyncTask(context,MainActivity.this).execute();
                swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        swipeContainer.post(new Runnable() {
                            @Override public void run() {
                                refreshContent();
                            }
                        });

                    }
                });

               // swipeContainer.setRefreshing(false);


            } else {

                AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
                builder1.setMessage("Cannot connect to Internet");
                builder1.setCancelable(true);

                builder1.setPositiveButton(
                        "OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert11 = builder1.create();
                alert11.show();

                Log.d(DEBUG_TAG,"No Network Connection");

            }


            //set on click listener
            final ListView lv= (ListView)findViewById(R.id.movies_list);;
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,long id)
                {

                    // selected item
                    TextView tvID=(TextView) view.findViewById(R.id.movie_ID);
                    String selectedMovieID = (tvID).getText().toString();
                    Intent intent = new Intent(context, MovieDetailsActivity.class);

                    intent.putExtra("movieID", selectedMovieID);
                    startActivity(intent);
                }
            });




        }
        catch(Exception ex)
        {
            Log.d(DEBUG_TAG,ex.getMessage());
        }

这是我更新视图的地方:

  public  void updateMainViewWithResults(ArrayList<MovieModel> result,MainActivity mainActivity) {
            try
            {
             //  swipeContainer.setRefreshing(false);
                //update  main activity here
                ListView listView = (ListView)  mainActivity.findViewById(R.id.movies_list);
                Log.d("DEBUG_TAG", result.toString());
                // Add results to listView.
                Context ctx=mainActivity.getContext();
                MoviesArrayAdapter adapter =
                        new MoviesArrayAdapter (mainActivity.getContext(), result,mainActivity);
                listView.setAdapter(adapter);
                if(listView.getParent()!=null)
                {
                    ((ViewGroup)listView.getParent()).removeView(listView);
                }
                // Update Activity to show listView
                mainActivity.setContentView(listView);

            }
            catch (Exception ex)
            {
                Log.d(DEBUG_TAG,ex.getMessage());
            }

        }

2 个答案:

答案 0 :(得分:0)

我认为你应该删除LinearLayout。

答案 1 :(得分:0)

ListView换成SwipeRefreshLayout。您的布局结构如下:

<LinearLayout>
  <SwipeRefreshLayout>
    <ListView/>
  </SwipeRefreshLayout>
</LinearLayout>