我使用SwipeRefreshLayout时遇到问题,功能完美无缺,但旋转器不会显示。我已经使用了所有提供的解决方案,但仍然没有。
我根本不想使用XML,所以我基本上编码所有内容并避免使用它。我有一个扩展SwipeRefreshLayout的类,我在其中创建listView,添加适配器并配置刷新操作等。我的最低API级别为16.
如果您需要任何进一步的细节或代码,请告诉我们,我很乐意提供给他们,我们非常感谢您的帮助!
我的活动
public class MainActivity extends Activity {
private static MainActivity mainActivity = null;
private MainView mainView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView = new MainView(this);
setContentView(mainView);
}
public static MainActivity getMainActivity() {
return mainActivity;
}
}
我的主要观点:
public class MainView extends SwipeRefreshLayout {
private ListView listView;
public MainView(Context context) {
super(context);
final String [] objects = new String[] {"panda", "horse", "fox"};
listView = new ListView(context);
final ArrayAdapter arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, objects);
listView.setAdapter(arrayAdapter);
addView(listView);
this.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
objects[0] = "Big panda!";
arrayAdapter.notifyDataSetChanged();
setRefreshing(false);
}
});
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int width = r - l;
int height = b - t;
listView.layout(0, 0, width, height);
}
}
答案 0 :(得分:0)
很难说没有看到任何代码,但我的直觉告诉我你需要确保链接了适当的进度视图,并且你正在将可见性设置为在适当的时间可见和不可见。
答案 1 :(得分:0)
这是一个简单的演示,在编码SwipeRefreshLayout
上取得了进展
每次滑动它都会在列表中添加一个项目
我的活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
CustomSwipeRefresh swipeRefresh = new CustomSwipeRefresh(this);
container.addView(swipeRefresh);
swipeRefresh.refreshData();
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context="com.divshark.customswiperefresh.MainActivity">
</RelativeLayout>
然后我创建了CustomSwipeRefresh:
public class CustomSwipeRefresh extends SwipeRefreshLayout {
private static final String TAG = CustomSwipeRefresh.class.getSimpleName();
private static int MIN_LIST = 50;
private List<String> mItems = new ArrayList<>();
private Context mContext;
private ListView listView;
private ListAdapter mAdapter;
public CustomSwipeRefresh(Context context){
super(context);
this.mContext = context;
mAdapter = new ListAdapter(mContext, mItems);
listView = new ListView(context);
listView.setAdapter(mAdapter);
listView.setBackgroundColor(Color.WHITE);
listView.setDivider(null);
listView.setDividerHeight(0);
addView(listView);
setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
setRefreshing(true);
postDelayed(new Runnable() {
@Override
public void run() {
onRefreshData(true);
Log.d(TAG,"--- FINISHED REFRESHING ---");
setRefreshing(false);
}
}, 2000);
}
});
}
public void refreshData(){
onRefreshData(false);
}
/**
* Called when the data is refreshed in the list
*/
private void onRefreshData(boolean addItem){
if(addItem){
MIN_LIST++;
}
mItems.clear();
for(int i = 0; i < MIN_LIST; i++){
mItems.add("New item "+ Integer.toString(i + 1));
}
mAdapter.notifyDataSetChanged();
}
public class ListAdapter extends BaseAdapter{
public List<String> mItems;
public Context mContext;
public LayoutInflater mInflater;
class Holder{
AppCompatTextView mTvItem;
}
public ListAdapter(Context context, List<String> items){
this.mContext = context;
this.mItems = items;
this.mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int position) {
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder ;
if(convertView == null){
holder = new Holder();
convertView = mInflater.inflate(R.layout.item_row, parent, false);
holder.mTvItem = (AppCompatTextView) convertView.findViewById(R.id.item);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
String item = mItems.get(position);
if(item != null){
holder.mTvItem.setText(item);
}
return convertView;
}
}
}
和项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="16dp">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/item"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
希望这有帮助!