2017-03-21 6 views
0

スティーブレイアウトのリスナーをfire.Iにすることはできません。例やその他の問題を見ましたが、何も問題がないように見えます。ここでSwipeLayoutリスナーを起動できません

は私activity.mainです:

<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()); 
      } 

     } 
+0

コード例を簡略化できますか? UIイベントを尋ねるときにAsyncTaskを実行する必要はありません。 –

+0

何をお勧めしますか?ハンドラ? – napoleon

+0

あなたの質問とは関係ないので、完全に削除することをお勧めします。 –

答えて

0

私はLinearLayoutを削除する必要があると思います。

0

ListViewSwipeRefreshLayoutで囲みます。レイアウト構成は次のようになります。

<LinearLayout> 
    <SwipeRefreshLayout> 
    <ListView/> 
    </SwipeRefreshLayout> 
</LinearLayout> 
+0

私はそれ以前に試してみました。まだ運がありません – napoleon

+0

もう一度試してみてください。私はこれを簡単なビューのデモと動作でテストします。 –

関連する問題